2

I have an array full of longitudes and latitudes. I have two double variables with my users location. I'd like to test the distance between my user's locations against my array to see which location is the closest. How do I do this?

This will get the distance between 2 location but stuggeling to understand how I'd test it against an array of locations.

CLLocation *startLocation = [[CLLocation alloc] initWithLatitude:userlatitude longitude:userlongitude];
CLLocation *endLocation = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];
CLLocationDistance distance = [startLocation distanceFromLocation:endLocation];
user3626407
  • 287
  • 1
  • 6
  • 15

2 Answers2

3

You just need to iterate through the array checking the distances.

NSArray *locations = //your array of CLLocation objects
CLLocation *currentLocation = //current device Location

CLLocation *closestLocation;
CLLocationDistance smallestDistance = DOUBLE_MAX;

for (CLLocation *location in locations) {
    CLLocationDistance distance = [currentLocation distanceFromLocation:location];

    if (distance < smallestDistance) {
        smallestDistance = distance;
        closestLocation = location;
    }
}

At the end of the loop you will have the smallest distance and the closest location.

Ian Warburton
  • 15,170
  • 23
  • 107
  • 189
Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • Whats the DOUBLE_MAX var used for? – user3626407 Jul 16 '14 at 15:05
  • DOUBLE_MAX sets the `smallestDistance` variable to the biggest possible number that it can be. You could set it to anything but if you set it to `100` initially then you would have a problem if the smallest distance from the array was actually `150`. – Fogmeister Jul 16 '14 at 15:16
  • @Fogmeister Shouldn't you be updating "smallestDistance" instead of "distance"? – Ian Warburton Nov 24 '15 at 21:35
2

@Fogmeister

I think this is a mistake which must be set right about DBL_MAX and an assignment.

First : Use DBL_MAX instead of DOUBLE_MAX.

DBL_MAX is a #define variable in math.h.
It's the value of maximum representable finite floating-point (double) number.

Second : In your condition, your assignment is wrong :

if (distance < smallestDistance) {
        distance = smallestDistance;
        closestLocation = location;
}

You must do :

if (distance < smallestDistance) {
        smallestDistance = distance;
        closestLocation = location;
}

The difference is that will be assign distance value into smallestDistance, and not the opposite.

The 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 is correct ?

Boris S.
  • 1,068
  • 1
  • 8
  • 18