如何测试两个CLLocations的相等性 [英] How do I test the equality of two CLLocations

查看:100
本文介绍了如何测试两个CLLocations的相等性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了isEqual问题:

I'm having a problem with isEqual:

代码:

 if (currentAnchor isEqual:currentBusiness.getCllLocation))
    {
        do a;
    }
    else
    {
        do b;
    }

currentanchor和currentbusiness.getCllocation是位置

currentanchor and currentbusiness.getCllocation are locations

但是如果它们相同,为什么要调用函数b?我的代码有问题吗?

But if they are the same, why is function b called? Is something wrong with my code?

推荐答案

我假定这两个对象都是基于getClLocation名称的CLLocation类型.

I assume both of these objects are of type CLLocation, based on the name of getClLocation.

CLLocation对其isEqual:方法的功能没有任何规范,因此它很可能只是继承了NSObject的实现,该实现只是比较对象的指针.如果您有两个具有相同数据的不同对象,则该isEqual:实现将返回NO.而且,如果您有两个不同的对象,只是位置略有不同,那么它们肯定是不相等的.

CLLocation doesn't have any specification on what its isEqual: method does, so it's likely just inheriting the implementation of NSObject, which simply compares the pointers of the objects. If you've got two distinct objects with identical data, that isEqual: implementation would return NO. And if you've got two distinct objects with just a slight variation in location, they definitely would not be equal.

比较位置对象时,您可能不希望isEqual:.相反,您可能想在CLLocation上使用distanceFromLocation:方法.这样的事情会更好:

You probably don't want isEqual: when comparing location objects. Rather, you probably want to use the distanceFromLocation: method on CLLocation. Something like this would be better:

CLLocationDistance distanceThreshold = 2.0; // in meters
if ([currentAnchor distanceFromLocation:currentBusiness.getCllLocation] < distanceThreshold)
{
  do a;
}
else
{
  do b;
}

这篇关于如何测试两个CLLocations的相等性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆