检测标记是否位于Google地图上的圆圈覆盖图(Javascript API V3) [英] Detect if marker is within circle overlay on Google Maps (Javascript API V3)

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

问题描述

我在地图上点缀了标记,并在标记您位置的标记上绘制半径(圆形覆盖图)(每次移动时都会更改)。
是否有任何方法可以检查其他标记是否进入圆圈?

更新



通过循环读取其他标记,并使用几何库计算标记和其他标记之间的距离,然后使用简单的if语句来查看它是否小于100米。

 函数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,所以如果你

解决方案

以下是一种添加的方法contains 方法添加到 google.maps.Circle 类中。它首先使用边界框排除点,如果它甚至不在边界框中。如果它位于边界框中,则它将从点到中心的距离与半径进行比较,并且仅当距离比半径短时才返回true。



一旦你添加下面的javascript,你可以调用你的circle对象的contains()方法。

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


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?

UPDATE

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++;
    });
}

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

解决方案

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.

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();
}

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

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