[英]Fit Bounds for Multiple Polygons google Maps
我正在嘗試擴展多個多邊形的地圖邊界,但似乎只是擴展了循環中最后一個多邊形的邊界。 關於我哪里出錯的任何建議?
function FitBounds(){
for (var i=0; i < shapes2.length; i++){
var paths = shapes2[i].getPaths();
var bounds= new google.maps.LatLngBounds();
paths.forEach(function(path){
var ar = path.getArray();
for(var i=0, l = ar.length; i <l; i++){
bounds.extend(ar[i]);
}
})
}
map.fitBounds(bounds)
}
在循環之外創建邊界。
function FitBounds(){
var bounds= new google.maps.LatLngBounds();
for (var i=0; i < shapes2.length; i++){
var paths = shapes2[i].getPaths();
paths.forEach(function(path){
var ar = path.getArray();
for(var i=0, l = ar.length; i <l; i++){
bounds.extend(ar[i]);
}
})
}
map.fitBounds(bounds)
}
僅供參考,谷歌地圖庫提供了一些輔助方法,可以更輕松地獲取形狀集合的邊界:
function FitBounds(){
var bounds = shapes2.reduce((boundsAccumulator, shape) =>
boundsAccumulator.union(shape.getBounds()), new google.maps.LatLngBounds())
map.fitBounds(bounds)
}
或者如果你不喜歡減少
function FitBounds(){
var bounds = new google.maps.LatLngBounds()
shapes2.forEach(shape) => bounds.union(shape.getBounds()))
map.fitBounds(bounds)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.