iPhone MKMapView:从当前位置检测数组中最近的位置

发布于 2024-10-01 17:20:23 字数 113 浏览 1 评论 0原文

我有一个包含大约 1,000 个对象的数组,每个对象都有一个精确的坐标值。我想做的是使用我拥有的“搜索”按钮,按下时检测当前位置并计算距离该位置最近的 10 个对象。对于解决这个问题的最佳方法有什么建议吗?谢谢。

I have an Array with approximately 1,000 Objects, each of which has a precise coordinate value. What I would like to do is take a Search button that I have and, when pressed, detect the current location and calculate 10 nearest Objects to this location. Any advice for the best way to go about this? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烈酒灼喉 2024-10-08 17:20:24

该公式仅适用于每个方向上的网格相同的平坦表面。在赤道的小比例尺上确实如此,但距离赤道越远,经度线彼此越近,而纬度线之间的距离仍然相同。

例如,从奥克兰出发,一级纬度约为 111.2 公里,一级经度约为 88.8 公里。所以网格不是正方形的。实际上意味着从 (0,0) 到 (0,1) 的距离比 (1,0) 更远。

正确的解决方案是使用 Apple 的 CLLocation 及其 getDistanceFrom: (或 distanceFromLocation: ),它根据球体表面计算真实距离(我认为使用 WGS84 投影)。

That formula only works for flat surfaces where the grids in each direction are the same. This is true at a small scale at the equator, but the further away from the equator you get the closer the lines of longitude get to each other, whilst the latitude lines are still the same distance apart.

For example from Auckland one degree of latitude is ~111.2Km and one of longitude is ~88.8Km. So the grid is not square. Effectively meaning that from (0,0) it is further to (0,1) than (1,0).

The proper solution is to use Apple's CLLocation and its getDistanceFrom: (or distanceFromLocation: ) which calculates true distances based on the surface of a sphere (using WGS84 projection I think).

眼泪都笑了 2024-10-08 17:20:24

马克,

是的,这很容易。您所要做的就是循环遍历数组并计算每个数组与您当前位置之间的距离。然后,您可以保留距离最近的十条记录。

距离公式就是:

( (x1 - x2)^2 + (y1 - y2)^2 ) ^ (1/2)

所以我在循环时有一个 NSMutableArray ,然后执行推送-弹出类型算法以返回最低的 10。

如果您需要坐标代码方面的帮助,请告诉我知道。

Mark,

Yes, this is quite easy. All you have to do is loop through the array and calculate the distance between each one and your present location. Then, you can keep the ten records with the lowest distance.

The distance formula is just:

( (x1 - x2)^2 + (y1 - y2)^2 ) ^ (1/2)

So I'd have an NSMutableArray as I looped, and then do a push-pop type algorithm to return the lowest 10.

If you need help with the coordinate code, let me know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文