【问题标题】:fitBounds and getZoom not playing nicefitBounds 和 getZoom 玩得不好
【发布时间】:2011-06-15 04:36:49
【问题描述】:

我有以下代码,我在地图初始化后几乎立即运行

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

问题是缩放检查和设置不起作用。我已经检查并启动了地图,但 map.getZoom() 函数此时返回未定义。

我可以做些什么来强制它等到设置了 fitbounds 并且知道缩放级别以便我可以适当地控制缩放?

【问题讨论】:

标签: javascript google-maps-api-3


【解决方案1】:
  // 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);
  }
}

【讨论】:

    【解决方案2】:

    我知道这是一个老问题,但我发现这个解决方案更优雅:

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

    【讨论】:

      猜你喜欢
      • 2019-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-25
      • 2011-10-14
      • 2015-07-06
      • 2022-08-21
      相关资源
      最近更新 更多