0

複数のマーカーでGoogleマップを表示するプロジェクトを開発しました。私の問題は、初期ロード時にすべてのマーカーが表示されるわけではないことです。すなわち。マーカーが 4 つある場合、マップが最初に読み込まれたときにマップに表示されるのは 2 つだけです。残りのマーカーを表示するにはズームアウトする必要があります。

どうすれば問題を解決できますか?

JavaScriptを介してGooglemapを取得するための私のコードを次に示します

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);                
        } 
    }       
4

2 に答える 2

2

Google Maps 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/を参照してください。

于 2013-04-02T11:11:20.520 に答える
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
于 2013-04-02T11:11:27.137 に答える