我正在尋找一種使用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);
}
- 3 回答
- 0 關注
- 891 瀏覽
添加回答
舉報
0/150
提交
取消