Google Maps setCenter


Center 1st worksI 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.
Center last breaksIn 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));
  }
}

4 Responses to “Google Maps setCenter”

  1. Anil Says:

    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);
    }

  2. KEINS Says:

    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.

  3. Russ Says:

    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.

  4. Christian Says:

    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!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.