-distanceFromLocation 中 CLLocation 对象的顺序:
这是我第一次在这里发布问题,但是我从其他人的问题中找到了很多帮助。
我的问题涉及 CLLocation
中的 -distanceFromLocation:
函数。我原以为求 A 点和 B 点之间的距离与求 B 点和 A 点之间的距离相同,但我发现事实并非如此。我在一个非常简单的情况下对此进行了测试:
CLLocation *test1 = [[CLLocation alloc] initWithLatitude:37.529530 longitude:-122.259232];
CLLocation *test2 = [[CLLocation alloc] initWithLatitude:47.900002 longitude:-103.495102];
NSLog(@"%f vs %f",[test2 distanceFromLocation:test1],[test1 distanceFromLocation:test2]);
[test1 release];
[test2 release];
输出为 1907269.942754 与 1908105.959114,相差近 900 米。
虽然 900 米可能只是一个很小的百分比,但我试图确定用户是否比注释更接近某些东西,而 900 米可以改变这一点。
我的问题是,这些值中哪一个(如果有)是正确的?另外,如果我测试的位置很重要,我是否需要始终针对同一位置进行测试(例如,用户到目的地和注释到目的地,而不是目的地到用户和注释到目的地)?
抱歉,该示例有点断章取义,因为我的项目相当大,这说明了我的问题。任何帮助将不胜感激,因为这给我带来了相当多的困惑和沮丧。
This is my first time posting a question here, but I have found a lot of help from other people's questions.
My question refers to the -distanceFromLocation:
function in CLLocation
. I had assumed that finding the distance between point A and point B would be the same as that between point B and point A, but I have found that it is not. I have tested this in a very simple case:
CLLocation *test1 = [[CLLocation alloc] initWithLatitude:37.529530 longitude:-122.259232];
CLLocation *test2 = [[CLLocation alloc] initWithLatitude:47.900002 longitude:-103.495102];
NSLog(@"%f vs %f",[test2 distanceFromLocation:test1],[test1 distanceFromLocation:test2]);
[test1 release];
[test2 release];
The output to this was 1907269.942754 vs 1908105.959114, a difference of almost 900 meters.
Though 900 meters may be a small percentage, I am trying to determine if the user is closer to something than an annotation and 900 meters can change that.
My question is, which one of these values, if any, is correct? Also if which location I test against is important, do I need to always test against the same location (e.g. user to destination and annotation to destination rather than destination to user and annotation to destination)?
Sorry that the sample is a bit out of context, as my project is rather large and this illustrates my problem. Any help would be appreciated as this has caused me quite a bit of confusion and frustration.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您观察到的误差是 2000 分之一。大概此方法中使用的算法针对速度进行了优化,因此按距离对位置列表进行排序很快,而不是精确到毫米。
如果您需要准确的结果,请不要使用这些方法,而应寻找其他方法。如果您需要可重现的结果,请以定义的方式对参数进行排序,例如始终从较低的纬度开始。
The error you're observing is 1 part in 2000. Presumably the algorithm used in this method is optimized for speed, so sorting a list of locations on distance is fast, rather than accurate to the millimeter.
If you need accurate results, don't use these methods but find another way. If you need reproducible results, order the arguments in a defined way, e.g. always start with the lower latitude.