google map v3如何缩放用户的位置

时间:2012-01-09 10:36:26

标签: google-maps google-maps-api-3 game-center fitbounds

我正在使用谷歌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);
    }

1 个答案:

答案 0 :(得分:0)

从当前位置删除setCenter。当地图的边界发生变化时,您需要有一个事件监听器。我想当你打电话给fitBounds时,你必须等待它重新绘制才能调整中心。一种方法是使用超时,但您只需将其添加到初始化函数中:

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

然后创建一个新函数来更新中心,它接受userPinLoc值(需要是一个全局变量):

function updateCenter() { 
    googleMap.setCenter(userPinLoc);
}
相关问题