Google maps 谷歌地图v3如何与用户缩放绑定';s位置

Google maps 谷歌地图v3如何与用户缩放绑定';s位置,google-maps,google-maps-api-3,game-center,fitbounds,Google Maps,Google Maps Api 3,Game Center,Fitbounds,我使用的是GoogleV3,我想在userPinLoc对象上使用它,我有以下代码 var bounds = new google.maps.LatLngBounds(); bounds.extend(userPinLoc)// wants to center on userPinLocation for (i in nearestEntitiesToZoom) { entity = nearestEntit

我使用的是GoogleV3,我想在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);
    }

将设置中心从当前位置移除。当映射的边界发生变化时,需要有一个事件侦听器。我认为当你调用fitBounds时,你必须等待它重新绘制,然后才能调整中心。一种方法是使用超时,但您可以简单地将其添加到初始化函数中:

google.maps.event.addDomListener(googleMap, 'bounds_changed', updateCenter);
然后创建一个新函数来更新中心,该函数接受userPinLoc值(需要是全局变量):


如果没有,请发布所有用于地图的JS。我再次看到这个问题,我发现制作中心是可行的,但如果任何pin脱离上下文,它将不会设置自动缩放级别。我已经更新了我的代码检查集InitialZoom Method,您以前接受了我对您问题的回答。你真的应该为另一期发布一个新问题,而不是仅仅编辑你的原始问题并取消接受我的答案。好的,很抱歉:)我创建了一个新问题,谢谢你的帮助亲爱的
function updateCenter() { 
    googleMap.setCenter(userPinLoc);
}