在 Objective-C 中实现一个简单的地理围栏
我正在尝试实现一些简单的地理围栏算法,该算法基本上执行以下操作:
- 假设我有两个点 A 和 B(每个点都有纬度和经度 地球价值)。
- 我可以从 A 点到 B 点画一条直线
- 我可以围绕该线设置一个周长,它是一个矩形(参见绘图 为了更清楚起见,请参见下文)
如果手机当前位置不在这个红色边界然后它会触发一些东西,基本上是一个代表。周长大小应该能够调整为百分比大小,因此 5% 是围绕线的小周长,70% 是围绕线的大周长。请注意,周长应该是矩形,而不是有半径的圆形。我猜构建这个过程会涉及到一堆 if 语句...如果有人能想出一个简单而优雅的解决方案(如果我能看到 Objective-C 中的代码那就太好了)那就太棒了。或者任何指导也会有帮助
I am trying to implement somewhat of a simple geofence algorithm that basically does the following:
- Say I have two point A and B (each point has a latitude and longitude
value in earth).- I can draw a straight line from point A to point B
- I can set a perimeter, which is a rectangle, around that line (see drawing
below for more clarity)
What I want to do is as follows, if the phone current location is outside of this red perimeter then it triggers something, basically a delegate. The perimeter size should be able to be adjusted to a percentage size, so 5% would be a small perimeter around the line and 70% would be a large perimeter around the line. Be aware that the perimeter should be a rectangle, not circle with radius. I am guessing that there will be a bunch of if statements involved in building this... if anyone could come up with a simple and elegant solution to this (would be great if I can see code in objective-C) that would be awesome. Or any guidance would be helpful as well
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以从矩形的四个点创建路径,然后使用
cgpathcontainspoint
检查当前位置是否在路径内。至于纬度和经度向平面X的转换,y坐标,最简单的解决方案是使用MAP套件使用Mercator投影。检查 了解地图几何有关更多信息。
下面是一个示例:
注意:此代码期望当前位置是一个点,而实际上,它是一个点和一个精度半径,实际上是一个圆。这会使事情有些复杂,因为现在您需要定义如何处理当前位置的情况恰好,但是您只知道它在圆圈中。如果矩形很大(例如5 km),则您可能只需要小于50m的准确性半径,就好像当前位置精确,并且忽略了计算的小不准确性。如果矩形较小(例如50m),则您也可以进行计算,就像当前位置是准确的一样,但是误报概率将更高(例如,有时会在矩形中检测到您,而您会站在外面的矩形的)。
或者,您可能想寻求“完美”解决方案并做圆形的交叉路口,这更复杂,不仅可能导致是和否答案,而且在此精度中也无法确定您是内部还是外部矩形”。
You can create a path from the four points of the rectangle and then use
CGPathContainsPoint
to check whether the current location is inside the path.As for the conversion of latitude and longitude to planar x, y coordinates, the simplest solution is to use Mercator projection using Map Kit. Check Understanding Map Geometry for more info.
Here's an example:
Note: This code expects that the current location is a point, while in reality, it is a point and a radius of accuracy, which is effectively a circle. This complicates things a bit because now you need to define how to handle situations when the current location is not known exactly, but you only know that it's somewhere in the circle. If the rectangle is large (say 5 km), then you may simply require radius of accuracy less than 50m, do the calculation as if the current location was exact and ignore the small inaccuracy of the computation. If the rectangle is smaller (say 50m), you may also do the calculation as if the current location was exact, but then the false positives probability would be higher (e.g. sometimes you would be detected as in the rectangle while you would be standing outside of it).
Or you may want to go for the "perfect" solution and do circle-rectangle intersection, which is more complex and may result not only in YES and NO answers but also in "with this accuracy it cannot be determined whether you are inside or outside of the rectangle".
您需要找到主 AB 线上距离用户位置最近的点。请查看以下链接以获取更多信息... 点线
现在假设您可以找到最近的点在从用户点(当前位置)开始的线上,您可以检查他们的位置与最近点之间的距离是否在您感兴趣的阈值内,如果超过阈值,则它们位于该线周围区域的“外部”。
You need to find the nearest point on the main A-B line to the users location. Look at the following link for more information... Point Line
Now given that you can find the nearest point on a line from users point (current location) you can check if the distance between their location and the nearest point is within the threshold you are interested in, if it exceeds it then they are 'outside' of the zone around the line.