为了账号安全,请及时绑定邮箱和手机立即绑定

Google Maps V3-如何计算给定范围的缩放级别

Google Maps V3-如何计算给定范围的缩放级别

API
qq_笑_17 2019-10-14 14:53:29
我正在寻找一种使用Google Maps V3 API计算给定范围的缩放级别的方法,类似于getBoundsZoomLevel()V2 API。这是我想做的:// These are exact bounds previously captured from the map objectvar sw = new google.maps.LatLng(42.763479, -84.338918);var ne = new google.maps.LatLng(42.679488, -84.524313);var bounds = new google.maps.LatLngBounds(sw, ne);var zoom = // do some magic to calculate the zoom level// Set the map to these exact boundsmap.setCenter(bounds.getCenter());map.setZoom(zoom);// NOTE: fitBounds() will not work不幸的是,我无法在fitBounds()特定的用例中使用该方法。它适用于在地图上拟合标记,但不适用于设置精确范围。这是为什么我不能使用该fitBounds()方法的示例。map.fitBounds(map.getBounds()); // not what you expect
查看完整描述

3 回答

?
长风秋雁

TA贡献1757条经验 获得超7个赞

对于API的第3版,这很简单且有效:


var latlngList = [];

latlngList.push(new google.maps.LatLng(lat, lng));


var bounds = new google.maps.LatLngBounds();

latlngList.each(function(n) {

    bounds.extend(n);

});


map.setCenter(bounds.getCenter()); //or use custom center

map.fitBounds(bounds);

和一些可选的技巧:


//remove one zoom level to ensure no marker is on the edge.

map.setZoom(map.getZoom() - 1); 


// set a minimum zoom 

// if you got only 1 marker or all markers are on the same address map will be zoomed too much.

if(map.getZoom() > 15){

    map.setZoom(15);

}


查看完整回答
反对 回复 2019-10-14
  • 3 回答
  • 0 关注
  • 803 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信