Find nearest location from existing coordinates mapkit

喜欢而已 提交于 2019-12-05 03:11:32

问题


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.


回答1:


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];
}


来源:https://stackoverflow.com/questions/8336203/find-nearest-location-from-existing-coordinates-mapkit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!