开发者

fitBounds and getZoom not playing nice

I have the following code which I am running almost immediately after map is i开发者_运维知识库nitialised

function showAddress(address) {
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.fitBounds(results[0].geometry.bounds);

        if (map.getZoom() < 12) {
            map.setZoom(12);
        }
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
  }

The problem is tha the zoom check and set doesn't work. I have checked and the map is initiated but the map.getZoom() function returns undefinted at this time.

Is there anything I can do to force it to wait until the fitbounds have been set and the zoom level is known so I can control the zoom appropriately?


  // Define the Bounds Changed event handler to do a "once-only" zoom level check
  google.maps.event.addListener(map, 'bounds_changed', function() {
    InitialZoomCheck()
  });

geocoder.geocode( { 'address': address}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
    map.fitBounds(results[0].geometry.bounds);




function InitialZoomCheck() {

  // Re-define the event handler so that this routine is called only once
  google.maps.event.addListener(map, 'bounds_changed', function() {
    document.posicion.z.value=map.getZoom()
  });

  if (map.getZoom() > 12) {
    map.setZoom (12);
  }
}


I know it's an old question, but i found this solution more elegant:

google.maps.event.addListenerOnce(map, 'bounds_changed', function() {
    if(map.getZoom() > 16) map.setZoom(16);
});
map.fitBounds(latlngbounds);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜