2013-04-02 35 views
0

我开发了一个项目,显示带有多个标记的谷歌地图。我的问题是,并非所有的标记都显示在初始加载。即。如果我有四个标记,地图最初加载时只有两个标记在地图中可见。我必须缩小以查看其余标记。如何在初始加载时在谷歌地图中显示所有标记?

我该如何解决这个问题?

我的继承人为通过JavaScript

window.onload=function(){     
     var mapOptions={     
      center:new google.maps.LatLng(markers[0].lat,markers[0].lng),     
      mapTypeId:google.maps.MapTypeId.ROADMAP   
     }; 
     var infoWindow=new google.maps.InfoWindow(); 
     var map=new google.maps.Map(document.getElementById("dvMap"),mapOptions); 
     var bounds=new google.maps.LatLngBounds(); 
     for(i=0;i<markers.length;i++){     
      var data=markers[i]; 
      var latlng=new google.maps.LatLng(data.lat,data.lng); 
      var marker=new google.maps.Marker({ 
       position:latlng, 
       map:map, 
       title:data.title    
      }); 
      (function(marker,data){ 
       google.maps.event.addListener(marker,"click",function(e){ 
        infoWindow.setContent(data.description); 
        infoWindow.open(map,marker); 
       });     
      })(marker,data);     
     } 
    }  

回答

2

得到的GoogleMap这已经有一段时间,因为我做了谷歌地图API的工作,但我相信你需要什么样的代码是这样的:

var bounds = new google.maps.LatLngBounds(); 

// Go through each... 
for (var i = 0, LtLgLen = LatLngList.length; i < LtLgLen; i++) { 
    // And increase the bounds to take this point 
    bounds.extend (LatLngList[i]); 
} 

// Fit these bounds to the map 
map.fitBounds (bounds); 

您已经定义绑定数组并循环您的标记,以将每个标记添加到边界数组,然后fitBounds。

查看原始文章的http://blog.shamess.info/2009/09/29/zoom-to-fit-all-markers-on-google-maps-api-v3/

1
for(i=0;i<markers.length;i++){     
      var data=markers[i]; 
      var latlng=new google.maps.LatLng(data.lat,data.lng); 
      var marker=new google.maps.Marker({ 
       position:latlng, 
       map:map, 
       title:data.title    
      }); 
      bounds.extend(marker); // here is the code to add every marker plotted on bounds 
      (function(marker,data){ 
       google.maps.event.addListener(marker,"click",function(e){ 
        infoWindow.setContent(data.description); 
        infoWindow.open(map,marker); 
       });     
      })(marker,data);     
     } 
map.setCenter(latlngbounds.getCenter()); // this will set the center of map to center of all markers 
map.fitBounds(latlngbounds); // this will fit all the markers to screen