缩放到多个标记边界不起作用

时间:2013-12-14 07:46:28

标签: google-maps google-maps-api-3 google-maps-markers

我想在地图上添加一些标记,这很好,但是我无法将地图设置为缩放到所有标记的边界(延伸)。你能告诉我我做错了吗?

// 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]
        });
        }

  });

1 个答案:

答案 0 :(得分: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