如何使用谷歌地图 v3 检查标记是否在边界内 [英] How can I check the marker is or isn't in the bounds using google maps v3

查看:41
本文介绍了如何使用谷歌地图 v3 检查标记是否在边界内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,我觉得可能有一些错误:

This is my code, I think it may has some mistakes:

        var bounds_array;
        google.maps.event.addListener(map,'bounds_changed', function (){
            var bounds_=map.getBounds();
            if(bounds_){
                var leftBottom=[bounds_.getSouthWest().lat(),bounds_.getSouthWest().lng()]
                var rightTop=[bounds_.getNorthEast().lat(),bounds_.getNorthEast().lng()]
                bounds_array=[leftBottom,rightTop];
            }
        });

    function check_is_in_or_out(marker){
        var leftBottom=bounds_array[0],rightTop=bounds_array[1];
        var marker_p=[marker.getPosition().lat(),marker.getPosition().lng()];
        if(marker_p[0]<leftBottom[0]||marker_p[0]>rightTop[0]||
            marker_p[1]<leftBottom[1]||marker_p[1]>rightTop[1])return 0;//0 is out
        else return 1;//1 is in
    }

这段代码足以检查边界是进还是出吗?

Is this code enough to check the bounds is in or out?

推荐答案

LatLngBounds 对象带有一个 contains() 方法,该方法采用 LatLng 点,如果该点恰好在边界内则返回 true,如果在边界外则返回 false.

The LatLngBounds object comes with a contains() method which takes a LatLng point and returns true if the point happens to be within the bounds, or false if outside.

那么,像下面这样的东西呢?

Therefore, what about something like the following?

function check_is_in_or_out(marker){
  return map.getBounds().contains(marker.getPosition());
}

这篇关于如何使用谷歌地图 v3 检查标记是否在边界内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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