objective-cxcodeiphone-4

How do I test the equality of two CLLocations


I'm having a problem with isEqual:

The code:

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

currentanchor and currentbusiness.getCllocation are locations

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


Solution

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

    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.

    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;
    }