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

我正在寻找一种使用Google Maps V3 API计算给定范围的缩放级别的方法,类似于getBoundsZoomLevel()V2 API。


这是我想做的:


// These are exact bounds previously captured from the map object

var 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 bounds

map.setCenter(bounds.getCenter());

map.setZoom(zoom);


// NOTE: fitBounds() will not work

不幸的是,我无法在fitBounds()特定的用例中使用该方法。它适用于在地图上拟合标记,但不适用于设置精确范围。这是为什么我不能使用该fitBounds()方法的示例。


map.fitBounds(map.getBounds()); // not what you expect


qq_笑_17
浏览 806回答 3
3回答

长风秋雁

对于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 centermap.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);}
打开App,查看更多内容
随时随地看视频慕课网APP