@Fogmeister
I think this is a bug that should be set correctly in relation to DBL_MAX and destination.
First: Use DBL_MAX instead of DOUBLE_MAX.
DBL_MAX is the #define variable in math.h.
This is the value of the maximum representable finite floating point number (double).
Second: In your state, your assignment is incorrect:
if (distance < smallestDistance) { distance = smallestDistance; closestLocation = location; }
You must do:
if (distance < smallestDistance) { smallestDistance = distance; closestLocation = location; }
The difference is that it will assign the distance value to the smallest distance, and not vice versa.
Final result:
NSArray *locations = //your array of CLLocation objects CLLocation *currentLocation = //current device Location CLLocation *closestLocation; CLLocationDistance smallestDistance = DBL_MAX; // set the max value for (CLLocation *location in locations) { CLLocationDistance distance = [currentLocation distanceFromLocation:location]; if (distance < smallestDistance) { smallestDistance = distance; closestLocation = location; } } NSLog(@"smallestDistance = %f", smallestDistance);
Can you confirm that this is correct?
source share