0
function ZoomCenter(bounds, canvas)
{
  var center = bounds.getCenter();

  if (canvas.getMapTypeId() == "roadmap") canvas.fitBounds(bounds);
  else
  {
    var maxZoomService = new google.maps.MaxZoomService();

    maxZoomService.getMaxZoomAtLatLng(center, function(response)
    {
      if (response.status != google.maps.MaxZoomStatus.OK) alert("Error in Google MaxZoomService");
      else
      {
        var max_zoom = (response.zoom > SATMAP_MAX_ZOOM) ? SATMAP_MAX_ZOOM : response.zoom;

        canvas.setOptions( { maxZoom: max_zoom } );
        canvas.fitBounds(bounds);
        canvas.setOptions( { maxZoom: SATMAP_MAX_ZOOM} );
      }
    });
  }
}

This first time this is called it zooms to 1 less than what will fit on the map. The second time and and every time after the first it will zoom to the correct level using the exact same bounds data.

Is there a bug in fitBounds? or am I missing something?

1 Answers1

0

Without seeing more code and having never experienced this quirk myself, it's difficult to say. However I am always more suspicious that there is a bug in my own code than in Google's, so I would eliminate the obvious possibilities there first.

Therefore, the first thing I would do is use a JS debugger to confirm that:

  • bounds is identical both times
  • it's going through the same execution path hitting the same canvas.fitBounds(bounds) call both times. (You have it in there twice. Which one gets executed depends on your conditional involving MapTypeId. If canvas has a different MapTypeId each time through, that could be causing the quirk.)

If that doesn't turn up the problem, the next place to look might be the Google Maps API issue tracker.

Trott
  • 66,479
  • 23
  • 173
  • 212
  • The map type is hybrib each time. The max zoom service returns 20 for the area that is checked. SATMAP_MAX_ZOOM is set to 20. The bounds data is the exact same each pass. The canvas is the same map each pass. The bounds data is the exact same each pass. – user1544767 Jul 23 '12 at 02:59
  • After more debugging I have concluded this is a bug in fitBounds. When the initial zoom level is 10 or less fitBounds returns 1 less than the correct zoom level. If the initial zoom level is 11 or more fitBounds returns the correct zoom level. – user1544767 Jul 23 '12 at 03:58
  • Interesting! If you haven't done so yet, I would encourage you to submit a bug report at the issue tracker link in my answer. – Trott Jul 23 '12 at 06:06
  • @user1544767 When you add a bug report, you must provide a link to a demonstrator (which needs to remain up for the duration of the bug report, possibly years). You may wish to edit your question to include that link, because then it can be checked and verified as a bug (rather than wait for Google, only to have them point out an error in your code). – Andrew Leach Jul 23 '12 at 06:58
  • I submitted a bug report already but I will make a make a page showing the bug and submit another bug report or edit the previous one and include a link. – user1544767 Jul 23 '12 at 16:44