自动调整缩放以适应Google地图中的所有标记 [英] Automatically adjust zoom to accommodate all marker in a google map
本文介绍了自动调整缩放以适应Google地图中的所有标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用最新版本的Google地图。如何使用经度和纬度添加标记并自动调整地图的缩放级别以包含使用JavaScript的所有标记? 解决方案
Google Maps API v3提供了一个 LatLngBounds
对象,您可以在其中添加多个 LatLng
对象。然后,您可以将此内容传递给 Map.fitBounds() code>
函数,如下所述:
var latlng = [
new google.maps.LatLng(1.23,4.56),
new google.maps.LatLng(7.89,1.01),
// ...
];
var latlngbounds = new google.maps.LatLngBounds();
for(var i = 0; i< latlng.length; i ++){
latlngbounds.extend(latlng [i]);
}
map.fitBounds(latlngbounds);
Using the latest version of Google maps. How to add markers using longitude and latitude and automatically adjust the zoom level of the map to include all the markers using JavaScript?
解决方案
Google Maps API v3 provides a LatLngBounds
object to which you can add multiple LatLng
objects. You can then pass this to Map.fitBounds()
function as described here:
- Zoom-To-Fit All Markers, Polylines or Polygons on A Google Map - API v2
- Zoom-to-Fit All Markers on Google Map v3
Partial Example
var latlng = [
new google.maps.LatLng(1.23, 4.56),
new google.maps.LatLng(7.89, 1.01),
// ...
];
var latlngbounds = new google.maps.LatLngBounds();
for (var i = 0; i < latlng.length; i++) {
latlngbounds.extend(latlng[i]);
}
map.fitBounds(latlngbounds);
这篇关于自动调整缩放以适应Google地图中的所有标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文