2012-05-31 37 views
1

我遇到問題。我有一系列經銷商的位置。它被稱爲locations。每組中的第一項是緯度,每組中的最後一項是經度。我找回了一張正確顯示所有位置的地圖,只是它放大了其中一個位置。它必須是一些界限,但我不知道是什麼。有人可以幫忙嗎?謝謝。GoogleMaps:地圖未擴展邊界以查看所有標記

這裏是我的代碼:

var map = new google.maps.Map(document.getElementById('map'), { 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }); 

var marker, i; 

if (locations.length > 1) { 
    var bounds = new google.maps.LatLngBounds(); 
    for (i = 0; i < locations.length; i++) { 
     marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(locations[i][0], locations[i][1]), 
        map: map 
       }); 
    } 

    bounds.extend(marker.getPosition());    

    map.fitBounds(bounds); 

回答

2

我想你想bounds.extend裏面的for循環。我看到它的方式,只是採取一個標記的位置。

+0

這樣做。謝謝,Heitor。 – sehummel

+0

不客氣! –

0

看起來,你的代碼你得到的最後一個標記的位置,並說地圖適合。你這樣做,與

bounds.extend(marker.getPosition());    

    map.fitBounds(bounds); 

,如果你想以適應所有標記,然後把

bounds.extend(m[i]); 

爲週期的標記之後。

類似的答案解釋得更好:Google Maps v3 API Extend Bounds. Javascript How-To?