Find closest longitude and latitude in array from user location
Asked Answered
S

2

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];
Subordinary answered 16/7, 2014 at 14:29 Comment(0)
A
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.

Atlanta answered 16/7, 2014 at 14:51 Comment(3)
Whats the DOUBLE_MAX var used for?Subordinary
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.Atlanta
@Atlanta Shouldn't you be updating "smallestDistance" instead of "distance"?Hendrix
A
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 ?

Adnopoz answered 24/4, 2015 at 11:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.