我正在使用谷歌地图通过标记显示位置。我正在将一组位置传递给 initialize()。我想设置自动缩放和自动居中来映射,以便所有标记都可见。 map 应采用缩放值,以便可以立即看到所有标记。 .这是我的代码:
function initialize(arr) {
var myOptions = {
zoom:13,
center: arr[0],
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
route = new google.maps.Polyline({
path: arr,
map:map
});
i=0;
interval = setInterval(function() {
if(i<=arr.length)
{
marker = new google.maps.Marker({
position: arr[i],
map: map
});
i++;
}//if
else if(i>arr.length){
window.clearInterval(interval);
}//else if
},1000);//interval
} //initialize
我有下面的代码,但不知道如何以及在哪里使用它。
var latlngbounds = new google.maps.LatLngBounds();
for (var i = 0; i < latlng.length; i++) {
latlngbounds.extend(latlng[i]);
}
map.setCenter(latlngbounds.getCenter(), map.getBoundsZoomLevel(latlngbounds));
最佳答案
从您的问题来看,您似乎使用了多个标记并且您想设置缩放级别。
如果是这样,我的建议是看一下 fitBounds()
.
fitBounds() method adjusts the map's viewport in order to view the passed LatLngBounds in full at the centre of the map.
查看此示例 fiddle用于演示。
关于javascript - 自动缩放和自动居中以在窗口中显示所有标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17939580/