Google Maps API v3 将地图重新​​定位到标记 [英] Google Maps API v3 recenter the map to a marker

查看:30
本文介绍了Google Maps API v3 将地图重新​​定位到标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果标记在地图之外,我正在寻找一种重新定位(聚焦)地图的方法.例如,如果您单击不在您的视图区域中的标记...标记 1:纽约标记 2:旧金山

i am searching for a way to recenter (focus) the map if an marker is outside the map. For example if you click on a marker which is not in your viewarea ... Marker 1: New York Marker 2: SanFransisco

在 V2 中我是这样做的……但是对于 v3, containsLatLng 需要一个额外的库并且对我不起作用……(请参阅我的其他帖子:Google Maps v3 map.getBounds().containsLatLng 不是函数) 他们还有其他聚焦方式吗到一个标记位置??

in V2 i make this way...but for v3 the containsLatLng needs an extra libary and did not work for me ...(see my other post: Google Maps v3 map.getBounds().containsLatLng is not a function) is they any other way to focus to a marker position??

更新:

        if ((!map.getBounds().contains(marker.getPosition())) & (showAllCategoryElements == 0)) {
        var newCenterPointLng = (map.getBounds().getCenter().lng() + marker.getPosition().lng()) / 2;
        var newCenterPointLat = (map.getBounds().getCenter().lat() + marker.getPosition().lat()) / 2;

        map.panTo(marker.getPosition());
        //map.setCenter(new google.maps.LatLng(newCenterPointLat, newCenterPointLng));

        if (!map.getBounds().contains(marker.getPosition())){
            map.zoomOut();
        } 
    }

推荐答案

你想要的方法是 contains(),而不是 containsLatLng().map.getBounds().contains(marker.getPosition())

The method you want is contains(), not containsLatLng(). map.getBounds().contains(marker.getPosition())

您可以使用地图的 setCenter() 或 panTo() 方法以标记位置为中心:map.setCenter(marker.getPosition())map.panTo(marker.getPosition())

You could use either the setCenter() or panTo() methods of the map to center on the marker position: map.setCenter(marker.getPosition()) or map.panTo(marker.getPosition())

因此,如果我正确理解您要执行的操作,它应该如下所示:

So if I understand correctly what you are trying to do, it should look like this:

if ((!map.getBounds().contains(marker.getPosition())) && (showAllCategoryElements == 0)) { //Note the double &  
    map.setCenter(marker.getPosition());  
    //OR map.panTo(marker.getPosition());  
}

这篇关于Google Maps API v3 将地图重新​​定位到标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆