How do I find the closest location from the user? Basically, I have a bunch of CLLocation
's, and I want to find the one closest to the user. I have retrieved the users current location but I want to find the closest CLLocation
. How would I go about doing this? You can just NSLog
the closest.
Asked
Active
Viewed 565 times
1

Eduardo
- 159
- 8
1 Answers
7
CLLocation *currentLocation;
NSArray *otherLocations;
CLLocation *closestLocation;
CLLocationDistance closestDistance = DBL_MAX;
for (CLLocation* location in otherLocations) {
CLLocationDistance distance = [currentLocation distanceFromLocation:location];
if (distance < closestDistance) {
closestLocation = location;
closestDistance = distance;
}
}
NSLog(@"the closest location is %@", closestLocation);

Kevin
- 16,696
- 7
- 51
- 68
-
2Need to update `closestDistance` when you update `closestLocation` – Firoze Lafeer Jul 27 '13 at 01:47