2010-04-27 22 views
0

我有一些jQuery代码,通过位置结果表并将相应的引脚放在地图上。我无法确定如何设置边界,以便在通过循环时在地图上生成标记并放大并平移以适应视图中的标记。我试过从本网站的一些类似问题实施代码,但似乎没有任何工作。请让我知道,我应该使用什么样的代码,并在那里到底我应该把它放在我的脚本:为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); 

     }); 
     }); 
}); 

`

回答

0

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

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

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