0

私はグーグルv3を使用しています、私はuserPinLocオブジェクトの中心に境界を合わせたいです、私は次のコードを持っています

 var bounds = new google.maps.LatLngBounds();
            bounds.extend(userPinLoc)// wants to center on userPinLocation
            for (i in nearestEntitiesToZoom) {
                    entity = nearestEntitiesToZoom[i];
                    var googleLatLng = new google.maps.LatLng(entity.latitude,entity.longitude);
                    bounds.extend(googleLatLng);
                }

                bounds.extend(userPinLoc);
                //googleMap.setCenter(userPinLoc) this not working

googleMap.fitBounds(bounds);

更新後のクイックフィックス新しいコードを貼り付けています

function setInitialZoom() {
        mapZoom = googleMap.getZoom(); 
        var bounds = new google.maps.LatLngBounds();
        bounds.extend(userPinLoc);
        for (i in nearestEntitiesToZoom) {
            entity = nearestEntitiesToZoom[i];
            var googleLatLng = new google.maps.LatLng(entity.latitude,entity.longitude);
            bounds.extend(googleLatLng);
        }

        google.maps.event.addDomListener(googleMap, 'bounds_changed', function() {
            googleMap.setCenter(userPinLoc);
        });

        googleMap.fitBounds(bounds);
        setTimeout(function() {
            google.maps.event.clearListeners(googleMap, 'bounds_changed');
        }, 3000);
    }
4

1 に答える 1

0

現在の場所からsetCenterを削除します。マップの境界が変更されたときのために、イベントリスナーが必要です。fitBoundsを呼び出すときは、中心を調整する前に、再描画されるのを待つ必要があると思います。1つの方法はタイムアウトを使用することですが、これを初期化関数に追加するだけです。

google.maps.event.addDomListener(googleMap, 'bounds_changed', updateCenter);

次に、センターを更新するための新しい関数を作成します。この関数は、userPinLoc値(グローバル変数である必要があります)を取得します。

function updateCenter() { 
    googleMap.setCenter(userPinLoc);
}
于 2012-01-09T10:46:03.167 に答える