从现有坐标mapkit查找最近的位置

sno*_*s74 4 objective-c mapkit ios

我正在为拥有多家商店的客户开发iphone应用程序(目标C).我有一个数组中所有商店(20)的坐标(纬度,长度).

目前我正在考虑循环遍历商店的坐标数组,并获取用户当前位置到商店位置的距离,然后将它们添加到数组中并对最低距离进行排序.

这是一种正确的方法还是非常耗费资源?

最近的SOF问题就是这个问题,但它是在python中: 提取最近的商店

提前感谢您的建议.

all*_*Nit 6

要在获取的数据中找到最近的位置,您需要计算从当前位置到您拥有的每个位置的距离.然后,只需对这些数据进行排序,您就可以从现有坐标mapkit获取最近的位置.

以下是找出距离的方法......

-(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];
}
Run Code Online (Sandbox Code Playgroud)