javascript 检查纬度和经度是否在谷歌地图的圆圈内
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24680247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if a latitude and longitude is within a circle google maps
提问by colourtheweb
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. 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.
我已经使用中心点 lat lang 和它周围的半径创建了圆。现在我想知道,如何检查(计算)纬度和经度是在该区域的内部还是外部,如果您能给我 Javascript 中的代码示例,我将不胜感激。我正在使用 Google Maps 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!!
任何帮助都会很棒.. 提前致谢!!
回答by Guffa
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 Pythagoran theoremcan be used to get the dinstance:
对于如此短的距离,并且当精度不必精确到厘米时,您可以将地球表面视为平坦的。在中心点的纬度计算度到公里的转换,然后可以使用勾股定理得到dinstance:
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. If that is the case, you would first have to normalise the longitudes.
注意:如果您传递的是 0/360 经度,则代码不会考虑在内。如果是这种情况,您首先必须对经度进行归一化。
回答by CoAstroGeek
All you need is a little spherical trig
所有你需要的是一个小的球形三角
First you need the central angle theta of the arcsubtended 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. Call this angle theta_p.
现在,如果兴趣点和您的中心点之间的中心角 < theta,则它在您的圆内。称这个角度为 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
编辑 - 抱歉,显然我不知道如何链接到图像?这是 URL: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. The included angle Cis 90 degrees (angle between lines of longitude and lines of latitude.
在这种情况下,球面三角形的两条边(称为a, b)分别是点的经度差和纬度差。夹角C为90度(经线与纬线的夹角。
The spherical trig law of cosines is:
余弦的球面三角定律是:
cos(c) = cos(a)*cos(b) + sin(a)*sin(b)*cos(C)
cis 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.
编辑 - 此解决方案不限于地球半径的小距离,就像其他建议一样。
回答by óscar Palacios
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.
使用勾股定理来验证。从中心到要验证的点的距离可以计算为三角形的斜边。