Google Maps API fitBounds 未正确设置缩放

Posted

技术标签:

【中文标题】Google Maps API fitBounds 未正确设置缩放【英文标题】:Google Maps API fitBounds doesn't set zoom correctly 【发布时间】:2013-03-28 22:58:56 【问题描述】:

我有一个问题,我认为 fitBounds 调用没有正确设置缩放级别。我已经设法在这里重现了我的测试用例http://tom.haddons.net/map.html - 基本上四个外部点是我设置的边界,如果你加载页面,然后放大一个级别,它们仍然可见。那么为什么谷歌地图不将缩放级别设置为呢?

谢谢,汤姆

【问题讨论】:

Looks like 顶部有边距,因此显示“默认”标记 How to affect the “grace margin” of map.fitBounds()的可能重复 【参考方案1】:

我通过以下方式设法解决了这个问题:

  var minlatminlong = new google.maps.LatLng( min_latitude ,  min_longitude );
  var minlatmaxlong = new google.maps.LatLng( min_latitude ,  max_longitude );
  var maxlatminlong = new google.maps.LatLng( max_latitude ,  min_longitude );
  var maxlatmaxlong = new google.maps.LatLng( max_latitude ,  max_longitude );

  zoomChangeBoundsListener = google.maps.event.addListener(map, 'bounds_changed', function()
      var bounds = this.getBounds();
      if ((bounds.getSouthWest().lng() >  min_longitude ) || (bounds.getNorthEast().lng() <  max_longitude ) || (bounds.getSouthWest().lat() >  min_latitude ) || (bounds.getNorthEast().lat() <  max_latitude )) 
          this.setZoom(this.getZoom() - 1);
       else 
          google.maps.event.removeListener(zoomChangeBoundsListener);
      
  );

请注意,包含在 '' 和 '' 中的任何内容都是我传递给 html 模板的变量。

【讨论】:

以上是关于Google Maps API fitBounds 未正确设置缩放的主要内容,如果未能解决你的问题,请参考以下文章

Google Maps API fitBounds 未正确设置缩放

Google Maps API v3:我可以在 fitBounds 之后设置缩放吗?

Google Maps API 3 fitBounds 填充 - 确保标记不会被覆盖控件遮挡

在 Google Maps API v3.0 中多次调用 map.fitBounds()

Google Maps API v3 在没有 fitbounds 的情况下无法加载,缩放导致无限循环/***

map.fitbounds google maps api v3 不适用于从 jQM 面板选择的位置