Reputation: 1307
I am in the process of developing an iphone app (objective C) for a client who has multiple stores. I have the coordinates (latitude,long) of all the stores (20) in an array.
At the moment I am thinking of looping through the array of stores' coordinates and get the distance from the user's current location to the store's location and then add them into array and does sorting on the lowest distance.
Is this a correct approach or very resource hungry?
The nearest question of SOF was this one but it is in python: extracting nearest stores
Thank you in advance for your advice.
Upvotes: 4
Views: 5487
Reputation: 5183
To find nearest location among fetched data, you need to calculate distance from current location to each location you are having. And then, just sort those data and you will get the nearest location from existing coordinate mapkit.
Following is a method to find out the distance...
-(float)kilometresBetweenPlace1:(CLLocationCoordinate2D) currentLocation andPlace2:(CLLocationCoordinate2D) place2
{
CLLocation *userLoc = [[CLLocation alloc] initWithLatitude:currentLocation.latitude longitude:currentLocation.longitude];
CLLocation *poiLoc = [[CLLocation alloc] initWithLatitude:place2.latitude longitude:place2.longitude];
CLLocationDistance dist = [userLoc getDistanceFrom:poiLoc]/(1000*distance);
NSString *strDistance = [NSString stringWithFormat:@"%.2f", dist];
NSLog(@"%@",strDistance);
return [strDistance floatValue];
}
Upvotes: 6