是否有任何简单的方法可以使用Google Maps API v3查找多段线的包围框?我正在研究一个项目,我需要在数据从地图中添加和删除时更新边界。这很简单,只要做bd.extend(point),其中bd是绑定对象,point是LatLng对象。问题是当我开始删除数据时,我希望它能够改变边界并放大回去。是否有内置的函数可以做到这一点,还是我需要为自己写点东西?在Google Maps API v3中获取polyine的界限
5
A
回答
4
v2 API有GPolyline.getBounds()
方法来做到这一点。但是,似乎v3 API中没有等效的方法。
您可能想要通过覆盖您的Polyline
MVCObject
的changed
属性来处理此问题,以便在对象更改状态时得到通知。然后,您可以使用您建议的LatLngBounds.extend()
方法计算边界框。
我认为谷歌有意从v3 API中省略了这些方法,试图保持API轻量级。类似的省略discussed a couple of days ago on Stack Overflow是GMap2.clearOverlays()
方法。
2
您还可以扩展Polyline类以添加您自己的getBounds方法。如何做到这一点,请参阅google maps api v3: add point on polyline beetwen two existing points on click polyline event。
7
在oenpelli的解决方案上进行了扩展,这是我用来从V2 API重新创建功能的扩展getBounds()方法。这在我的项目中完美运作。
google.maps.Polyline.prototype.getBounds = function() {
var bounds = new google.maps.LatLngBounds();
this.getPath().forEach(function(item, index) {
bounds.extend(new google.maps.LatLng(item.lat(), item.lng()));
});
return bounds;
};
请记住,这需要在加载API之后添加,因此在您的init方法中。
相关问题
- 1. 重置Google Maps API v3上的界限
- 2. 在Google Maps API V3中获取非包裹地图边界
- 3. Google Maps v3 API扩展界限map.fitBounds无法扩展界限
- 4. Google Maps API V3:向全世界展示
- 5. Google Maps API v2 vs Google Maps API v3?
- 6. Google Maps API V3上的限制标记
- 7. Google Maps API v3 BrowserIsCompatible
- 8. Google Maps API v3 - maxZoomService
- 9. Google Maps API V3:fromContainerPixelToLatLng
- 10. 在Google Maps API v3中获取InfoWindow的锚点对象
- 11. Google maps API V3方法fitBounds()
- 12. google maps api v3 + infoBubble in markerClusterer
- 13. kml + google maps api v3 + geoxml3
- 14. Google Maps API V3 - Javascript问题
- 15. Google Maps API V3 - Internet Explorer
- 16. Google maps api v3 + marker clusterer:centering map?
- 17. Google Maps Api v3 - getBounds is undefined
- 18. Google Maps API v3覆盖
- 19. Google Maps API V3返回610,0,0,0
- 20. Google Maps API v3 directions.route()请求
- 21. 在Google Maps API V3中使用Choropleth
- 22. 在Google Maps API V3中绘制标记
- 23. Google Maps API v3:为什么Google Maps API V3和Google Maps的搜索结果之间存在不一致?
- 24. Safari 6.0.2中的Google Maps API v3 *破损*
- 25. Google Maps API v3中的路线距离
- 26. 使用Google Maps API v3在边界框内显示标记
- 27. 获取相对于屏幕的标记坐标Google Maps API v3
- 28. Google Maps API v3街景“google未定义”
- 29. 在Unity 3D上使用Google Maps API v3
- 30. 在Google Maps API v3中获取自定义切片的中心LatLng点