Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps setCenter()

I'm using google maps. In my code i've used setCenter() function. My problem is that marker is always located on top left corner of map area (not at the center). Please tell me how to resolve it?

My piece of code is

lat = 46.437857; lon = -113.466797;  marker = new GMarker(new GLatLng(lat, lon));   var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(20, 40)); map.addControl(new GLargeMapControl3D(), topRight); map.setCenter(new GLatLng(lat, lon), 5);  map.addOverlay(marker); 
like image 540
hotcoder Avatar asked May 05 '10 12:05

hotcoder


People also ask

How do I reference Google Maps?

The required elements for referencing from Google Maps are: Map publisher (Year of publication) Title of map section, Sheet number or tile, scale. Available at: DOI or URL (Accessed: date).


2 Answers

@phoenix24 answer actually helped me (whose own asnwer did not solve my problem btw). The correct arguments for setCenter is

map.setCenter({lat:LAT_VALUE, lng:LONG_VALUE}); 

Google Documentation

By the way if your variable are lat and lng the following code will work

map.setCenter({lat:lat, lng:lng}); 

This actually solved my very intricate problem so I thought I will post it here.

like image 142
Hammad Khan Avatar answered Sep 20 '22 19:09

Hammad Khan


 function resize() {         var map_obj = document.getElementById("map_canvas");        /*  map_obj.style.width = "500px";         map_obj.style.height = "225px";*/         if (map) {             map.checkResize();             map.panTo(new GLatLng(lat,lon));         }     }  <body onload="initialize()" onunload="GUnload()" onresize="resize()"> <div id="map_canvas" style="width: 100%; height: 100%"> </div> 

like image 42
hotcoder Avatar answered Sep 20 '22 19:09

hotcoder