寻找圆内的坐标
我正在 Grails 下使用 Google 地图进行混搭,用户可以通过选择一个点来创建 地理围栏地图和半径。 该数据存储在我的数据库中,并且应用程序不断从 GPS 设备接收一组坐标。
我想将接收到的坐标与圆圈中存储的区域进行比较。 如果该点在圆的内部(或外部),程序将触发一个动作。 但是,我想知道如何找出坐标是否位于圆内/圆外。 有一个 Javascript 库允许执行此操作,但我需要在服务器上执行此操作。
是否有一个 Java(甚至 Groovy)库可以实现这个目的? 你会如何实施它?
I am doing a mashup using Google Maps under Grails where users can create geofences by selecting a point on the map and a radius. This get stored on my database and the application receives constantly a set of coordinates from a GPS device.
I would like to compare the received coordinates with the area stored in the circles. If the point is inside (or outside) the circle the program will fire an action. However, I would like to know how I can find out if the coordinates are located inside/outside the circle. There is a Javascript library which allows doing this but I need to do this on the server.
Is there a Java (or even Groovy) library for this?
How would you implement it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果从点到圆心的距离<= 圆半径,那么它在圆内。
如果该区域由多个圆组成,那么与所有圆进行比较……不会花那么长时间。
java.awt.geom.Point2D.Double 非常适合此目的。
if distance from point to center of circle is <= radius of circle then it is inside the circle.
if the area is made of more than one circle than compare to all the circles... it won't take that long.
java.awt.geom.Point2D.Double is perfect for this.
好吧,如果它不需要“完美”,你就不需要担心绘制圆圈或类似的东西。 您只需获取两个位置(要测试的位置和圆心)并使用毕达哥拉斯即可找到距离。 如果该距离小于圆的半径,则它在圆内。
然而,有一个警告需要考虑:这并不完美的原因是,对于你的点,你可能会得到纬度和经度......而地球是一个球体。 因此,在地球两极附近,这会崩溃。 但对于你正在做的事情来说,它可能已经足够好了。
Well, if it doesn't need to be "perfect", you don't need to worry about plotting circles or anything like that. You can just take the two locations (the location you want to test, and the center of the circle) and use Pythagorus to find the distance. If that distance is less than the radius of the circle, it's inside.
There is a caveat to take into consideration, however: the reason this wouldn't be perfect is that that for your points, you're probably going to get a latitude and longitude...and the Earth is a sphere. So near the poles of the Earth this will kind of fall apart. But it may well be good enough for what you're doing.
遗憾的是,这里的大多数回复对您来说并不方便,因为 GPS 坐标以度为单位。 您将需要一些东西来将纬度和经度的两个点转换为大圆距离,这是简单的毕达哥拉斯定理所达不到的。
如果您使用的是 Google 地图 API,则可以使用 GLatLng。 正如其他海报所指出的,您可以确定两点之间的距离小于指定圆的半径。 具体来说,GLatLng.distance(other:GLatLng) 返回 GPS 位置之间的米距离。
要真正显示圆圈需要更多的技巧。 您需要创建一个 GPolygon 来绘制圆的周长。 您可以找到许多可以为您执行此操作的免费 JavaScript 函数。
Sadly, most of the responses here won't work for you conveniently, because GPS coordinates are in units of degrees. You will need something to convert from two points in Degrees of latitude and longitude to a great circle distance, which simple Pythagorean theorem falls short of.
If you're using Google maps API, you can probably do everything you need using GLatLng. As other posters have noted, You can determine the distance between two points is less than the radius of the specified circle. Specifically GLatLng.distance(other:GLatLng) returns the meters distance between too GPS locations.
To actually display the circles requires a bit more finesse. You will need to create a GPolygon to draw the circumference of the circle. You can find a number of free JavaScript functions that can do this for you.
维克多和贝斯卡给出了正确答案。 也就是说,如果点到圆心的距离小于半径,则它在圆内。
对于两点之间的大圆距离,您可以使用GeoTools'
大地测量计算器
。 特别是,您可以使用setStartingGeographicPoint
和setDestinationGeographicPoint
设置点和半径,然后调用getOrthodromicDistance
这将返回距离。Victor and Beska have the correct answer. That is, if the distance between the point and the center is less than the radius, then it's in the circle.
For the great circle distance between two points, you can use GeoTools'
GeodeticCalculator
. In particular you set the point and radius usingsetStartingGeographicPoint
andsetDestinationGeographicPoint
followed by callinggetOrthodromicDistance
which will return the distance.您想要找到所选坐标与圆心之间距离的向量,然后通过对向量分量进行平方并将它们相加来计算所选坐标与圆心之间的平方距离; 如果该标量(距离的平方)小于半径的平方,则该点在圆内。
这种方法避免了取平方根,并且与正常距离比较一样准确。
You want to find the vector that is the distance between the selected coordinate and the center of the circle, then compute the square distance between the selected coordinate and the center of the circle by squaring the components of the vector and adding them together; if that scalar (the squared distance) is less than the square of the radius, the point is within the circle.
This method avoids having to take a square root, and is just as accurate as normal distance comparison.
一种可能性是计算距中心点的距离并将其与半径进行比较。
根据您的应用程序,您可能必须考虑到世界是一个球体而不是二维的。 要计算地球上的距离,您可以使用此公式。
One possibility is to calculate the distance from the centerpoint and compare it to the radius.
Depending on you application you may be have to take into account that the world is a sphere and not 2Dimensional. To calcualte a distance on earth you can use this formula.
由于您使用的是谷歌地图,并且对于地理距离而言,球面几何而不是欧几里德几何成立。 但是,如果距离相对较小,例如停车场等,那么您可以使用欧几里德距离公式(http:// /en.wikipedia.org/wiki/Distance)来确定该点是在圆内还是在圆外。
我假设您知道圆心 C(xc, yc) 的坐标及其半径 R。然后对于给定点 P(x1, y1) 找到欧几里德距离 D 作为
平方根((x1-xc)^ 2 + (y1-yc)^2))。 如果D> R,点位于圆外。 如果D< R,点位于圆内。 如果 D = R,则该点位于圆的圆周上。
如果您要进行较大距离的测量,那么您应该寻找测地线(请检查此 http ://en.wikipedia.org/wiki/Great-circle_distance)。
我希望它有帮助。
干杯
Since you are using Google Maps and for geographical distances spherical geometry holds rather than euclidean geometry. However if it is relativley smaller distance like a parking lot etc. then you can use euclidean distance formula (http://en.wikipedia.org/wiki/Distance) to find out whether the point is inside or outside the circle.
I presume you know the coordinates of the circle's center C(xc, yc) and its radius, R. Then for a given point P(x1, y1) find the euclidean distance, D as
square-root((x1-xc)^2 + (y1-yc)^2)). If D > R, the point lies outside the circle. If D < R, the point lies inside the circle. If D = R, the point lies on the circumference of the circle.
In case you are doing your measurements over larger distances then you should rather look for Geodesics (please check this http://en.wikipedia.org/wiki/Great-circle_distance).
I hope it helps.
cheers