0
我一直在尋找一種方法來覆蓋Google地圖的中心latlng並使用fitBounds()方法進行縮放。正如你所看到的,我有一個latlng座標數組,我希望能夠以正確的縮放比例在地圖中找到所有座標。令人煩惱的是,地圖只在初始化地圖時纔會採用中心屬性的latlng座標。請幫忙,我看不出我做錯了什麼。謝謝如何用fitBounds()覆蓋Google Map初始化的opts;
function initialize() {
var centrelatlng = new google.maps.LatLng(52.474, -1.868);
var myOptions = {
zoom:11,
center: centrelatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var image = 'http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png';
var hotels = [
['ibis Birmingham Airport', 52.452656, -1.730548, 4],
['ETAP Birmingham Airport', 52.452527, -1.731644, 3],
['ibis Birmingham City Centre', 52.475162, -1.897208, 2]
];
var latLngs = [];
var hotel;
for (var i = 0; i < hotels.length; i++) {
hotel = hotels [i];
var myLatLng = new google.maps.LatLng(hotel[1], hotel[2]);
latLngs[i] = myLatLng;
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
icon: image,
title: hotel[0],
zIndex: hotel[3]
});
}
var latlngbounds = new google.maps.LatLngBounds();
latLngs.each(function(n){
latlngbounds.extend(n);
});
map.setCenter(latlngbounds.getCenter());
map.fitBounds(latlngbounds);
};
請注意,您必須向下滾動代碼查看器以查看所有代碼。
棒極了,修好了。感謝puckhead :) – user1437606