检测标记是否在谷歌地图上的圆形覆盖范围内(Javascript API V3) [英] Detect if marker is within circle overlay on Google Maps (Javascript API V3)

查看:25
本文介绍了检测标记是否在谷歌地图上的圆形覆盖范围内(Javascript API V3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在地图周围点缀了标记,在标记您位置的标记上有一个半径(圆圈叠加)(每次移动都会改变).有什么办法可以检查其他标记是否在圆圈内?

I have markers dotted around a map, and a radius (circle overlay) on a marker marking your location (which changes every time you move). Is there any way I can check to see if the other markers come inside the circle?

更新

我通过遍历彼此的标记来解决这个问题,并使用几何库计算您的标记与另一个标记之间的距离,然后使用一个简单的 if 语句来查看它是否小于 100 米.

I got around this by looping through each other marker, and using the geometry library calculating the distance between your marker and the other marker and then a simple if statement to see if it's less than 100 meters.

function checkAllChests() {
    var Current = 0;
    $.each(treasureArray, function() {
        //var thisLocation = treasureArray[Current].getPosition();

        var distanceBetween = Math.ceil(google.maps.geometry.spherical.computeDistanceBetween(treasureArray[Current].getPosition(), marker_me.getPosition()));
        if(distanceBetween < 100) {
            alert('CAN OPEN THIS CHEST');
        }
        Current++;
    });
}

我想指出,上面的代码使用了 jQuery,所以如果您不使用 jQuery,它将无法工作.

I'd like to note that the above code uses jQuery, so if you aren't using jQuery it won't work.

推荐答案

这是一种将 contains 方法添加到 google.maps.Circle 类的方法.它首先使用边界框排除一个点,如果它甚至不在边界框中.如果在边界框内,则将点到中心的距离与半径进行比较,只有当距离小于半径时才返回true.

Here's a way to add a contains method to the google.maps.Circle class. It first uses the bounding box to exclude a point if it's not even in the bounding box. If it is in the bounding box, then it compares the distance from the point to the center with the radius, and returns true only if the distance is shorter than the radius.

添加下面的 javascript 后,您可以在圆对象上调用 contains() 方法.

Once you add the javascript below, you can call the contains() method on your circle object.

google.maps.Circle.prototype.contains = function(latLng) {
  return this.getBounds().contains(latLng) && google.maps.geometry.spherical.computeDistanceBetween(this.getCenter(), latLng) <= this.getRadius();
}

这篇关于检测标记是否在谷歌地图上的圆形覆盖范围内(Javascript API V3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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