简体   繁体   English

检查纬度和经度是否在一个圆圈内谷歌地图

[英]Check if a latitude and longitude is within a circle google maps

Below is the desired result, which I'm looking for以下是我正在寻找的理想结果

在此处输入图像描述

What I would like to know is:我想知道的是:

I have created the circle using center point lat lang and radius around it.我使用中心点 lat lang 和围绕它的半径创建了圆。 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.现在我想知道,如何检查(计算)纬度和经度是否在区域内或区域外,如果您能给我 Javascript 中的代码示例,我将不胜感激。 I'm using Google Maps API V3.我正在使用谷歌地图 API V3。

I found this function but not working as expected for me:我找到了这个功能,但没有按预期工作:

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

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

Note: The code doesn't take into consideration if you are passing the 0/360 longitude.注意:如果您传递的是 0/360 经度,则代码不会考虑在内。 If that is the case, you would have to normalize the longitudes first.如果是这种情况,您必须首先对经度进行归一化。

All you need is a little spherical trig你所需要的只是一个小球三角

First you need the central angle theta of the arc subtended by your distance (L = 10 km).首先,您需要距离(L = 10 公里)所对圆弧的中心角 theta。

L = theta*r

where r is the radius of the earth (6378.135 km)其中 r 是地球的半径(6378.135 公里)

Now, if the central angle between the point of interest and your center point is < theta, it is inside your circle.现在,如果兴趣点和您的中心点之间的中心角 < theta,则它在您的圆内。 Call this angle theta_p.将此角度称为 theta_p。

Here's a diagram illustrating a spherical triangle: spherical triangle image http://en.wikipedia.org/wiki/File:Spherical_trigonometry_basic_triangle.svg这是一个说明球面三角形的图表: 球面三角形图像 http://en.wikipedia.org/wiki/File:Spherical_trigonometry_basic_triangle.svg

edit - sorry, apparently I don't know how to link to an image??编辑 - 抱歉,显然我不知道如何链接到图像? Here's the URL: http://en.wikipedia.org/wiki/File:Spherical_trigonometry_basic_triangle.svg这是网址: http : //en.wikipedia.org/wiki/File : Spherical_trigonometry_basic_triangle.svg

In this case, two of the sides of the spherical triangle (call them a, b ) are the difference in longitude and difference in latitude of the points respectively.在这种情况下,球面三角形的两条边(称为a, b )分别是点的经度差和纬度差。 The included angle C is 90 degrees (angle between lines of longitude and lines of latitude.夹角C为 90 度(经线与纬线之间的夹角。

The spherical trig law of cosines is:余弦的球面三角定律是:

cos(c) = cos(a)*cos(b) + sin(a)*sin(b)*cos(C)

c is the central angle between your points, which we earlier called theta_p c是您的点之间的中心角,我们之前称之为 theta_p

edit - this solution isn't limited to small distance WRT the radius of the earth, as the other suggestions are.编辑 - 这个解决方案不限于小距离 WRT 地球的半径,就像其他建议一样。

Use the Pythagorean theorem to validate.使用勾股定理来验证。 The distance from your center to the point you want to validate can be calculated as the hypotenuse of a triangle.从中心到要验证的点的距离可以计算为三角形的斜边。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM