I have an array of CLLocation
s which represent the route user calculated.
User has an option to add certain POIs to that route and I need to know the way to tell where exactly on the route that POI will be.
What I came up with is next:
I would go through my route array, and grab a pair of CLLocations and calculate a distance from my POI to both of those coordinates and save that value. I can do so for all pairs of coordinates on the route, and then see which distance is the smallest and that is how I would know if my coordinate is between two other ones.
Is there any other method of doing it but this one ?
This is how you can achieve this:
- (CLLocation*)closestLocationToLocation:(CLLocation*)currLocation {
CLLocationDistance minDistance;
CLLocation *closestLocation = nil;
for (CLLocation *location in arrayOfLocations) {
CLLocationDistance distance = [location distanceFromLocation:currLocation];
if (distance <= minDistance || closestLocation == nil) {
minDistance = distance;
closestLocation = location;
}
}
return closestLocation;
}