1

取得したいくつかのマーカーがあり、マップを中央に配置したいと考えています。はい、他のすべての回答を読みましたが、うまくいかないようです: Google Map API v3 - set bounds and center

JS:

geocoder.geocode( {'address': loc}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var markerBounds = new google.maps.LatLngBounds();
            var coordinate = results[0].geometry.location;
            var icon = new google.maps.MarkerImage("images/icon-"+type+".png", new google.maps.Size(37, 44));   

            //create the marker
            var marker = new google.maps.Marker({
                map: map, 
                position: coordinate,
                visible: true,
                id: type,
                shadow: shadow,
                icon: icon
            });
            markerBounds.extend(coordinate);
            map.fitBounds(markerBounds);
        }
}
4

2 に答える 2

1

コードは常に、最後のマーカーである 1 つのポイントのみの LatLngBounds を使用して fitBounds を呼び出しています...その関数を複数回呼び出している場合、呼び出すたびに、最後のマーカーの fitBounds になります。geocoder.geocode 関数の外側で markerBounds 変数を定義できるため、その値が保持されます。

var markerBounds = new google.maps.LatLngBounds();
geocoder.geocode( {'address': loc}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var coordinate = results[0].geometry.location;
            var icon = new google.maps.MarkerImage("images/icon-"+type+".png", new google.maps.Size(37, 44));   

            //create the marker
            var marker = new google.maps.Marker({
                map: map, 
                position: coordinate,
                visible: true,
                id: type,
                shadow: shadow,
                icon: icon
            });
            markerBounds.extend(coordinate);
            map.fitBounds(markerBounds);
        }
}

これで、markerBounds は一度初期化され、新しいマーカーごとに拡張されます。

于 2012-06-29T15:49:35.017 に答える