我如何知道 Lat,Lng 点是否包含在圆内?
好的,非常不言自明。我正在使用谷歌地图,我试图找出纬度、经度点是否在半径为 x 的圆内(x 由用户选择)。
边界框对此不起作用。我已经尝试使用以下代码:
distlatLng = new google.maps.LatLng(dist.latlng[0],dist.latlng[1]);
var latLngBounds = circle.getBounds();
if(latLngBounds.contains(distlatLng)){
dropPins(distlatLng,dist.f_addr);
}
这仍然会导致标记位于圆圈之外。
我猜这是一些简单的数学,需要计算曲率或面积,但我不知道从哪里开始。有什么建议吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不幸的是,毕达哥拉斯在球体上没有帮助。因此斯图尔特·比尔德的答案是不正确的;经度差与米没有固定的比率,而是取决于纬度。
正确的方法是使用大圆距离公式。假设地球是球形的,一个很好的近似是这样的(在 C++ 中):
其中
和
Unfortunately Pythagoras is no help on a sphere. Thus Stuart Beard's answer is incorrect; longitude differences don't have a fixed ratio to metres but depend on the latitude.
The correct way is to use the formula for great circle distances. A good approximation, assuming a spherical earth, is this (in C++):
where
and
使用 Google Maps API 几何库计算圆心与标记之间的距离,然后将其与半径进行比较。
Use Google Maps API geometry library to calculate distance between circle's center and your marker, and then compare it with your radius.
这很简单。您只需计算中心和给定点之间的距离并将其与半径进行比较。您可以从此处获取计算两个纬度之间距离的帮助
It's very simple. You just have to calculate distance between centre and given point and compare it to radius. You can Get Help to calculate distance between two lat lang from here
以下代码适用于我:我的标记无法拖动到圆外,而是仅悬挂在其边缘(向任何方向),并保留最后一个有效位置。
该函数是标记“拖动”事件的事件处理程序。
感谢 http://unserkaiser.com/code/google-地图标记检查如果在圆圈/
和 http://www.mvjantzen.com/blog/?p=3190 。
The following code works for me: my marker cannot be dragged outside the circle, instead it just hangs at its edge (in any direction) and the last valid position is preserved.
The function is the eventhandler for the markers 'drag' event.
Thanks to http://unserkaiser.com/code/google-maps-marker-check-if-in-circle/
and http://www.mvjantzen.com/blog/?p=3190 .
我确实有点傻了。思考一下我们可以利用毕达哥拉斯定理。
我们有距某个点的最大距离(X 英里)、两个纬度和两个经度。如果我们用这些形成一个三角形,那么我们就可以求解到该点的距离。
假设我们知道坐标
lat1,lng1
的point1
是圆心,坐标lat2,lng2
的point2
> 是我们试图确定的点是否在圆内。我们使用由
point1
和point2
确定的点形成一个直角三角形。point3
将具有坐标lat1,lng2
或lat2,lng1
(无论是哪一个)。然后我们计算差异(或者如果您愿意)距离 -latDiff = lat2-lat1
和lngDiff = lng2-lng1
然后我们使用毕达哥拉斯计算距中心的距离 - <代码>dist=sqrt(lngDiff^2+latDiff^2)。
我们必须将所有内容转换为米,以便它能够在 google 地图上正常工作,因此英里乘以 1609(大约),纬度/经度乘以 111000(大约)。这并不完全准确,但它做得足够了。
希望一切都有意义。
I've been a bit silly really. Thinking about it we can use Pythagorus' theorem.
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.
So say we know
point1
with coordinateslat1,lng1
is the center of the circle andpoint2
with coordinateslat2,lng2
is the point we are trying to decide is in the circle or not.We form a right angled triangle using a point determined by
point1
andpoint2
. This,point3
would have coordinateslat1,lng2
orlat2,lng1
(it doesn't matter which). We then calculate the differences (or if you prefer) distances -latDiff = lat2-lat1
andlngDiff = lng2-lng1
we then calculate the distance from the center using Pythagorus -
dist=sqrt(lngDiff^2+latDiff^2)
.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.
Hope that all makes sense.