문제

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.

도움이 되었습니까?

해결책

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top