如何测试两个CLLocations的相等性 [英] How do I test the equality of two 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屋!