views:

248

answers:

2

Hi everyone,

I develop a simple Google Maps application. I need just to place one marker on the map. For whatever reason, the marker is placed outside of the visible area of the map. It's a little bit strange, because the map is centered to the marker's coordinates. Here is the code:

var point1 = new GLatLng(location1.lat,location1.lon);    
    map1.setCenter(point1, 15);
var marker1 = new GMarker(point1);
map1.addOverlay(marker1);
map1.setCenter(point1);

When we drag the map a little bit, we can see the marker. What do I need is to center the map in the way the marker will be visible without map dragging.

Can anyone help me?

A: 

Check this code out:

var map = new GMap(document.getElementById("map"));
/* -- snip -- */
map.centerAndZoom(new GPoint(-1.2736, 53.0705), 8);

From a website I made a while ago. Feel free to check the source:

http://www.primrose-house.co.uk/localattractions

Just click the link in the top right to switch to the map view.

Robert Grant
+1  A: 

I believe the GLatLng object would accept String arguments as well - but to be safe I would ensure that they are integers - try using:

new GLatLng(parseInt(location.lat), parseInt(location.lon));

I also noticed you call map.setCenter a second time which ought to not be necessary.

Using the following code really ought to do it

  map=new GMap(document.getElementById("map"));
  var point = new GLatLng(parseInt(location.lat), parseInt(location.lon));
  map.setCenter(point,5);
  var marker = new GMarker(point);
  map.addOverlay(marker);

If you still are having issues I would check that "location" object to make sure the .lat and .lon values are being populated correctly.

John