Google maps 谷歌地图仅在模式中不以标记为中心

Google maps 谷歌地图仅在模式中不以标记为中心,google-maps,google-maps-markers,Google Maps,Google Maps Markers,看看这个JSFIDLE。查看地图如何偏离标记的中心?现在看一看同一张地图,当它不在模态中时,它就工作了。发生什么事了 function initialize() { var addresses = $('.tracking-map-marker-details'); window.map = new google.maps.Map(document.getElementById('map_canvas_trackingmap'), { mapTypeId: google.map

看看这个JSFIDLE。查看地图如何偏离标记的中心?现在看一看同一张地图,当它不在模态中时,它就工作了。发生什么事了

function initialize() {
  var addresses = $('.tracking-map-marker-details');

  window.map = new google.maps.Map(document.getElementById('map_canvas_trackingmap'), {
    mapTypeId: google.maps.MapTypeId.HYBRID
  });

  var infowindow = new google.maps.InfoWindow(),
      bounds = new google.maps.LatLngBounds(),
      geocoder = new google.maps.Geocoder();

    $(addresses).each(function() {
        var address  = $(this).data('address'),
            letter   = $(this).data('letter'),
            status   = $(this).data('status');

      geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          var marker = new google.maps.Marker({
            icon: "http://www.google.com/mapfiles/marker" + letter + ".png",
            shadow: 'http://www.google.com/mapfiles/shadow50.png',
            position: results[0].geometry.location,
            map: map
            });

            bounds.extend(results[0].geometry.location);
        }
      });
    });

  var listener = google.maps.event.addListenerOnce(map, "idle", function () {
    map.fitBounds(bounds);
    map.setZoom(5);

    $('#tracking_map_modal').modal('show').on('shown', function () {
      google.maps.event.trigger(map, 'resize');
    });
  });     
}

google.maps.Map fitBounds
将仅确保地图的视口包含给定的边界

您想打电话给,传入标记的
latLng

if (status == google.maps.GeocoderStatus.OK) {
    // var marker = ...;
    window.map.setCenter(results[0].geometry.location);
    // ...
}

答案是在显示modal(模式)后向setCenter(设置中心)发送

$('#tracking_map_modal').modal('show').on('shown', function () {
      google.maps.event.trigger(map, 'resize');
      map.setCenter(bounds.getCenter());
 });

您好,我希望贴图基于边界居中。怎么做?你已经在做了。“边界”由构成边界框的四个点组成。谷歌地图通过跟踪边界框的东北角和西南角来处理这些问题。你看到JSFIDLE了吗?地图偏离了标记的中心。如果您可以使用工作示例更新JSFIDLE,那么这将是非常好的。我已经给了您所需要的答案。您已经在设置视口边界。如果你想以记号笔为中心,请使用我答案中的代码。否则,请澄清你的问题。事实上,不,这不是答案。地图已在标记上居中,但没有此标记。仅当贴图处于模式时,问题才会出现