Reputation: 423
I'm creating an application which needs to determine if my current location (recd in didUpdateLocation:) is present among a set of geo-coordinates I have. Now I understand that comparing double/float values for equality can be error prone as the precisions involved in geo-coordinates is very high. Slight inaccuracy in the GPS can throw my algo off-track. Hence I need to compare it with some error margin.
How do I compare 2 CLLocations with a margin of error? Or determine the error in the location reading (I don't think this is possible since CLLocationManager would have rectified it).
Thanks in advance :)
Upvotes: 2
Views: 1957
Reputation: 2440
In each CLLocation
instance there is a property declared as @property(readonly, nonatomic) CLLocationAccuracy horizontalAccuracy;
What you can do is to make a routine like:
-(BOOL)isLocation:(CLLocation*)location inRangeWith:(CLLocation*)otherLocation{
CLLocationDistance delta = [location distanceFromLocation:otherLocation];
return (delta < location.horizontalAccuracy);
}
You can make even more complex logic as both locations have that property...
Cheers. 【ツ】
Upvotes: 4
Reputation: 15722
You can use the CoreLocation method distanceFromLocation:
to find the distance between the user's location and another point. If the distance is less than whatever threshold you decide, then you can assume the locations are the same.
CLLocationDistance threshold = 5.0; // threshold distance in meters
// note: userLocation and otherLocation are CLLocation objects
if ([userLocation distanceFromLocation:otherLocation] <= threshold) {
// same location
}
Upvotes: 3