我怎么知道一个 Lat,Lng 点是否包含在一个圆内? [英] How do I know if a Lat,Lng point is contained within a circle?
问题描述
好的,不言自明.我正在使用谷歌地图,我试图找出一个经纬度点是否在半径圆内,比如 x(x 由用户选择).
Ok pretty self explanatory. I'm using google maps and I'm trying to find out if a lat,long point is within a circle of radius say x (x is chosen by the user).
边界框不适用于此.我已经尝试使用以下代码:
Bounding box will not work for this. I have already tried using the following code:
distlatLng = new google.maps.LatLng(dist.latlng[0],dist.latlng[1]);
var latLngBounds = circle.getBounds();
if(latLngBounds.contains(distlatLng)){
dropPins(distlatLng,dist.f_addr);
}
这仍然会导致标记位于圆圈之外.
This still results in markers being places outside the circle.
我猜这是一些需要计算曲率或面积的简单数学,但我不确定从哪里开始.有什么建议吗?
I'm guess this is some simple maths requiring the calculation of the curvature or an area but I'm not sure where to begin. Any suggestions?
推荐答案
我真的有点傻.考虑一下,我们可以使用毕达哥拉斯定理.
I've been a bit silly really. Thinking about it we can use Pythagorus' theorem.
我们有一个距离一个点的最大距离(X 英里),以及两个纬度和两个经度.如果我们使用这些形成一个三角形,那么我们就可以求解到该点的距离.
We have a maximum distance away from a point (X miles), and two latitudes and two longitudes. If we form a triangle using these then we can solve for the distance from the point.
所以说我们知道 point1
坐标为 lat1,lng1
是圆的中心,point2
坐标为 lat2,lng2
是我们试图决定是否在圆圈中的点.
So say we know point1
with coordinates lat1,lng1
is the center of the circle and point2
with coordinates lat2,lng2
is the point we are trying to decide is in the circle or not.
我们使用由 point1
和 point2
确定的点形成一个直角三角形.这 point3
将具有坐标 lat1,lng2
或 lat2,lng1
(哪个无关紧要).然后我们计算差异(或者如果您愿意)距离 - latDiff = lat2-lat1
和 lngDiff = lng2-lng1
We form a right angled triangle using a point determined by point1
and point2
. This, point3
would have coordinates lat1,lng2
or lat2,lng1
(it doesn't matter which). We then calculate the differences (or if you prefer) distances - latDiff = lat2-lat1
and lngDiff = lng2-lng1
然后我们使用毕达哥拉斯计算距中心的距离 - dist=sqrt(lngDiff^2+latDiff^2)
.
we then calculate the distance from the center using Pythagorus - dist=sqrt(lngDiff^2+latDiff^2)
.
我们必须将所有内容都转换为米,以便它在谷歌地图上正常工作,因此英里乘以 1609(大约),纬度/经度乘以 111000(大约).这并不完全准确,但它做得很好.
We have to translate everything into meters so that it works correctly with google maps so miles are multiplied by 1609 (approx) and degrees of latitude/longitude by 111000 (approx). This isn't exactly accurate but it does an adequate job.
希望一切都有意义.
这篇关于我怎么知道一个 Lat,Lng 点是否包含在一个圆内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!