3

我在下面有一段代码,它使用 bounds.extend() 和 map.fitBounds() 来调整地图的大小以适应所有标记。我希望地图可以以 start_point 为中心并缩小到适当的水平,以便看到每个标记。

但是,它最终会最大程度地放大到 start_point。而且我尝试(commen)不每次在 geocoder.geocode 回调函数中调用 bounds.extend() ,而是将标记添加到数组中并在单独的循环中调用 bounds.extend() ,这也不起作用。

我仔细检查了标记是否已成功创建,如果我手动缩小,我可以看到它们。

mark_pins() 作为 ajax 成功回调函数被调用,这里我没有包含。

我想念什么吗?

   var map;
   var start_point = new google.maps.LatLng(37.519002, -122.131);
   var bounds = new google.maps.LatLngBounds();


   function initialize() {
    var map_canvas = document.getElementById('map_canvas');

    var map_options = {
      center: start_point,
      zoom: 10,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(map_canvas, map_options);

  }

  google.maps.event.addDomListener(window, 'load', initialize);


  function mark_pins(trucks){
    var geocoder = new google.maps.Geocoder();
    var markersArray = [];


    for (i = 0; i < trucks.length; i++) {
      // iterate each truck address
      geocoder.geocode( { 'address' : trucks[i]['address']}, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
              var marker = new google.maps.Marker({
                  map: map,
                  position: results[0].geometry.location
              });
              marker.setMap(map);
              bounds.extend(results[0].geometry.location);
              //markersArray.push(marker);

          } else {
              alert('Internal error: ' + status + address);
          }
      });
    }
    var bounds = new google.maps.LatLngBounds();


    for (i = 0; i< markersArray.length; i++) {
      //code
      //bounds.extend(new google.maps.LatLng(markersArray[i][1], markersArray[i][2]));
    }
    bounds.extend(start_point);
    map.setCenter(start_point);

    map.fitBounds(bounds);


  }
4

1 回答 1

7

地理编码器是异步的。map.fitBounds(bounds)在结果返回之前调用您的代码。发布的代码也从不调用 mark_pins 函数。

function mark_pins(trucks) {
    var geocoder = new google.maps.Geocoder();
    var markersArray = [];


    for (i = 0; i < trucks.length; i++) {
        // iterate each truck address
        geocoder.geocode({
            'address': trucks[i]['address']
        }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location
                });
                marker.setMap(map);
                bounds.extend(results[0].geometry.location);
                map.fitBounds(bounds);
            } else {
                alert('Internal error: ' + status + address);
            }
        });
    }
}

工作小提琴

结果地图的屏幕截图

代码片段:

var map;
var start_point = new google.maps.LatLng(37.519002, -122.131);
var bounds = new google.maps.LatLngBounds();
var trucks = [{
    address: "Sunnyvale, CA"
}, {
    address: "Palo Alto, CA"
}, {
    address: "Reno, NV"
}, {
    address: "Portland, OR"
}];

function initialize() {
    var map_canvas = document.getElementById('map_canvas');

    var map_options = {
        center: start_point,
        zoom: 10,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(map_canvas, map_options);
    mark_pins(trucks);
}

google.maps.event.addDomListener(window, 'load', initialize);


function mark_pins(trucks) {
    var geocoder = new google.maps.Geocoder();
    var markersArray = [];


    for (i = 0; i < trucks.length; i++) {
        // iterate each truck address
        geocoder.geocode({
            'address': trucks[i]['address']
        }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location
                });
                marker.setMap(map);
                bounds.extend(results[0].geometry.location);
                //markersArray.push(marker);
                map.fitBounds(bounds);
            } else {
                alert('Internal error: ' + status + address);
            }
        });
    }
}
html, body, #map_canvas {
    height: 100%;
    width: 100%;
    padding: 0px;
    margin: 0px;
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map_canvas"></div>

于 2014-09-02T10:03:13.053 回答