【问题标题】:Set bounds for markers generated by jQuery table loop?为 jQuery 表循环生成的标记设置边界?
【发布时间】:2010-04-27 22:56:42
【问题描述】:

我有一些 jQuery 代码,它遍历位置结果表并将相应的图钉放在地图上。我无法弄清楚如何设置边界,以便当它通过循环并在地图上生成标记时,它会缩放和平移以适应视图中的标记。我已经尝试从这个站点上的一些类似问题中实现代码,但似乎没有任何效果。请让我知道我应该使用什么代码以及我应该把它放在我的脚本中的什么地方:

$(function() {
   var latlng = new google.maps.LatLng(44, 44);

   var settings = {
      zoom: 15,
      center: latlng,
      disableDefaultUI: false,
      mapTypeId: google.maps.MapTypeId.ROADMAP
   };

   var map = new google.maps.Map(document.getElementById("map_canvas"), settings);

      $('tr').each(function(i) {

         var the_marker = new google.maps.Marker({
            title: $(this).find('.views-field-title').text(),
            map: map,
            clickable: true,
            position: new google.maps.LatLng(
               parseFloat($(this).find('.views-field-latitude').text()),
               parseFloat($(this).find('.views-field-longitude').text())
            )
         });

         var infowindow = new google.maps.InfoWindow({
            content:  $(this).find('.views-field-title').text() +
                      $(this).find('.adr').text()
         });

         new google.maps.event.addListener(the_marker, 'click', function() {
            infowindow.open(map, the_marker);

         });
      });
});

`

【问题讨论】:

    标签: jquery loops google-maps-api-3


    【解决方案1】:

    var map = ... 下添加var bounds = new google.maps.Bounds(latlng, latlng);

    然后在var the_marker = ... 下的循环中添加bounds.extend(the_marker.getPosition());

    然后是循环结束添加map.fitBounds(bounds);

    【讨论】:

      猜你喜欢
      • 2018-07-28
      • 2015-06-28
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 2014-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多