I wasted a bit of time trying to figure out why my 5 lines of Google map JavaScript was not working. I created a simple Google map to center the map on a given point and add a marker overlay. It appears that the order of setting the center and the adding the overlay is critical. In the first case, I set the center and added the overlay and the result is as expected.
In the second case, if I add the overlay before I center the map, I get a blank map — and on Firefox with Firebug installed I see that following error:
this.k has no properties (main.js line 542)
L.prototype.Na=function(a){return this.k.getProjection().fromLatLngToPixel(a,thi...
Here is what the Google Map API documentation has to say about setCenter method: Sets the map view to the given center. Optionally, also sets zoom level and map type. The map type must be known to the map. See the constructor, and the method addMapType(). This method must be called first after construction to set the initial state of the map. It is an error to call other operations on the map after construction.
Here is the correct way of constructing a map:
function load() {
if (GBrowserIsCompatible()) {
var ct = new GLatLng(37.4419, -122.1419);
var map = new GMap2(document.getElementById("map"));
map.setCenter(ct, 12);
// now add add the overlays ...
map.addOverlay(new GMarker(ct));
}
}
October 15, 2008 at 1:25 pm |
hi,
i am trying to add a marker at the center of the map. But its weird that the marker is not getting displayed at the center. Not atleast on the boundries of the map, but just at the edge of the north-west corner. here is the peice of the code i wrote.
if (GBrowserIsCompatible()) {
var map = new GMap(document.getElementById(“map”));
map.setCenter(new GLatLng(17.502030, 78.382801), 12);
var marker = new GMarker(new GLatLng(17.502030, 78.382801));
map.addOverlay(marker);
}
January 15, 2009 at 1:39 pm |
Hi, I m having the same problem as Anil. My google map is also showing marker at north-west corner. My code is the same as Anil’s.
May 1, 2009 at 6:44 pm |
RE: Anil and KEINS questions, I know this is several months late, but have you tried putting the map on its own page without any stylesheets? Does that fix it?
Several of the elements on google maps can be affected by stylesheets.
It’s a pretty easy starting point towards possibly fixing positioning issues.
June 19, 2009 at 8:23 pm |
Thanks very much for this fix. Saved my arse when it came down to deadline time, and my damn map was going all gray.
You the man!