如何使用谷歌地图 v3 检查标记是否在边界内

编程入门 行业动态 更新时间:2024-10-11 01:13:56
本文介绍了如何使用谷歌地图 v3 检查标记是否在边界内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

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屋!

更多推荐

[db:关键词]

本文发布于:2023-03-31 21:58:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/815269.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:边界   如何使用   标记   地图

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!