检查纬度和经度是否在谷歌地图的圆圈内 [英] Check if a latitude and longitude is within a circle google maps

查看:26
本文介绍了检查纬度和经度是否在谷歌地图的圆圈内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我想要的结果

我想知道的是:

我使用中心点纬度和半径创建了圆.现在我想知道,如何检查(计算)纬度和经度是在该区域的内部还是外部,如果您能给我 Javascript 中的代码示例,我将不胜感激.我使用的是 Google Maps API V3.

I have created the circle using center point lat lang and radius around it. Now I want to know, how to check (calculate) if a latitude and longitude is either inside or outside the area I would appreciate if you can give me code example in Javascript. I'm using Google Maps API V3.

我发现了这个功能,但没有按预期工作:

function arePointsNear(checkPoint, centerPoint) {
    var sw = new google.maps.LatLng(centerPoint.lat() - 0.005, centerPoint.lng() - 0.005);
    var ne = new google.maps.LatLng(centerPoint.lat() + 0.005, centerPoint.lng() + 0.005);
    var bounds = new google.maps.LatLngBounds(sw, ne);
    if (bounds.contains (checkPoint)){
        return true;
    }
    return false;
}

任何帮助都会很棒..提前致谢!!

Any help will be great.. thanks in advance!!

推荐答案

对于如此短的距离,并且当精度不必精确到厘米时,您可以将地球表面视为平坦的.计算中心点纬度处度数到公里的换算,则勾股定理可得用于获取距离:

For such short distances, and when the accuracy doesn't have to be exact to the centimeter, you can treat the surface of the earth as flat. Calculate a conversion from degrees to kilometers at the latitude of the center point, then the Pythagorean theorem can be used to get the distance:

function arePointsNear(checkPoint, centerPoint, km) {
  var ky = 40000 / 360;
  var kx = Math.cos(Math.PI * centerPoint.lat / 180.0) * ky;
  var dx = Math.abs(centerPoint.lng - checkPoint.lng) * kx;
  var dy = Math.abs(centerPoint.lat - checkPoint.lat) * ky;
  return Math.sqrt(dx * dx + dy * dy) <= km;
}

演示:http://jsfiddle.net/Guffa/57gQa/

注意:如果您传递的是 0/360 经度,则代码不会考虑在内.如果是这种情况,您必须先对经度进行归一化.

Note: The code doesn't take into consideration if you are passing the 0/360 longitude. If that is the case, you would have to normalize the longitudes first.

这篇关于检查纬度和经度是否在谷歌地图的圆圈内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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