谷歌地图v3如何通过缩放用户位置进行fitbounds

发布于 2024-12-25 10:38:06 字数 1357 浏览 5 评论 0原文

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

I am using google v3, i want to fitbounds with center on userPinLoc object, i have the following code

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

any quick fix after update i am pasting new code

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不一样的天空 2025-01-01 10:38:06

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

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

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

function updateCenter() { 
    googleMap.setCenter(userPinLoc);
}

Remove the setCenter from where it is currently. You need to have an event listener for when the map's bounds change. I think when you call fitBounds, you have to wait for it to redraw before you can adjust the centre. One way would be to use a timeout, but you can simply add this to your initialize function:

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

And then create a new function to update the centre, which takes the userPinLoc value (needs to be a global variable):

function updateCenter() { 
    googleMap.setCenter(userPinLoc);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文