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

查看:61
本文介绍了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需要一个额外的库并且对我不起作用...(请参阅我的其他文章:

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天全站免登陆