2013-12-14 44 views
0

我正在添加一些標記到地圖,這是工作正常,但我不能設置地圖縮放到所有標記的邊界(擴展)。你能讓我知道我做錯了什麼嗎?放大到多個標記界限不起作用

// Multiple Markers 
var markers = [ 
    ['Tacloban', 11.247524,125.00463], 
    ['Guiuan', 11.034212,125.731659], 
    ['Ormoc', 11.026798,124.600754], 
    ['Cebu', 10.31019,123.881836], 
    ['Roxas', 11.59036,122.763977] 

]; 
    google.maps.event.addDomListener(document.getElementById('alloffice'), 'click', function() { 
    map.setCenter(latlngOfficeAall); 
    map.setZoom(8); 
// Loop through our array of markers & place each one on the map 
    for(i = 0; i < markers.length; i++) { 
     var position = new google.maps.LatLng(markers[i][1], markers[i][2], markers[i][3], markers[i][4], markers[i][5]); 
     bounds.extend(position); 
     marker = new google.maps.Marker({ 
      position: position, 
      map: map, 
      title: markers[i][0] 
     }); 
     } 

    }); 
+0

,以適應範圍從哪裏調用[map.fitBounds(https://開頭開發商。 google.com/maps/documentation/javascript/reference#Map)? – geocodezip

回答

2

你需要告訴地圖中心和縮放與map.fitBounds(bounds)

// Multiple Markers 
var markers = [ 
    ['Tacloban', 11.247524,125.00463], 
    ['Guiuan', 11.034212,125.731659], 
    ['Ormoc', 11.026798,124.600754], 
    ['Cebu', 10.31019,123.881836], 
    ['Roxas', 11.59036,122.763977] 

]; 

google.maps.event.addDomListener(document.getElementById('alloffice'), 'click', function() { 
    var bounds = new google.maps.LatLngBounds(); 
    // Loop through our array of markers & place each one on the map 
    for(i = 0; i < markers.length; i++) { 
    var position = new google.maps.LatLng(markers[i][1], markers[i][2], markers[i][3], markers[i][4], markers[i][5]); 
    bounds.extend(position); 
    marker = new google.maps.Marker({ 
     position: position, 
     map: map, 
     title: markers[i][0] 
    }); 
    } 
    map.fitBounds(bounds); 
}); 

working example

+0

嗨geocodezip,我更新我的代碼,因爲你評論,但它仍然沒有縮放到所有標記的邊界。我以爲我應該刪除map.setCenter(latlngOfficeAall);和map.setZoom(8);來自代碼的線條,但地圖仍然沒有放大到標記的擴展! – user1760110

+0

您是否修復了javascript錯誤和/或定義了所有必需的外部變量?當我修復它對我有用(它沒有被測試,因爲你沒有提供完整的代碼)。 – geocodezip

+0

感謝geocodezip – user1760110