CLLocation - 将 2km 或 2000m 半径添加到坐标
如何向 CLLocation
坐标添加半径?我想要的是查看特定坐标附近有哪些客户。
我需要的是将坐标添加 2000 米或 2 公里。
我该怎么做?
How can I add a radius to a CLLocation
coordinate? What I want is to see what clients are near a specific coordinate.
What I need is to add 2000 meters, or 2km to a coordinate.
How do I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能为坐标“添加半径”。如果您有 CLLocation 对象(例如 loc1 和 loc2),您可以做的就是计算它们之间的距离:
并查看 dist(以米为单位)是否大于或小于 2000.0。
坐标纬度/经度(极坐标)和距离之间的转换很复杂,这就是 SDK 为您提供此功能的原因。
You can't "add a radius" to a coordinate. What you can do if you have
CLLocation
objects (say loc1 and loc2), is calculate the distance between them:and see if dist (which is in meters) is more or less than 2000.0.
Converting between coordinate latitudes/longitudes (which are polar coordinates) and distances is complicated, which is why the SDK provides you with this functionality.
由于距离很小,您可以使用等距柱状投影距离近似。这种近似比使用半正矢公式更快。因此,要获取从参考点 (lat1/lon1) 到您正在测试的点 (lat2/lon2) 的距离,请使用以下公式。重要提示:您需要将所有纬度/经度点转换为弧度:
如果 d 小于 2,则您的点距离参考点 2 公里以内。
为了有效地遍历您的点,请按经度对点进行排序。相距 2 度的经度将大于 2 公里(除非您靠近杆子),因此您不需要循环遍历这些经度。
Since the distance is small, you can use the equirectangular distance approximation. This approximation is faster than using the Haversine formula. So, to get the distance from your reference point (lat1/lon1) to the point your are testing (lat2/lon2), use the formula below. Important Note: you need to convert all lat/lon points to radians:
If d is less than 2, then your point is within 2km of your reference point.
To efficiently march through your points, order the points on longitude. Longitudes that are 2 degrees away will be greater than 2km (unless you are near a pole), therefore you don't need to loop through those.