2011-12-01 61 views
4

我正在为拥有多个商店的客户开发iPhone应用程序(目标C)。我有一个数组中的所有商店(20)的坐标(纬度,长度)。从现有坐标查找最近的位置mapkit

目前我正在考虑循环访问商店的坐标数组,并获取从用户当前位置到商店位置的距离,然后将它们添加到数组中,并在最小距离上进行排序。

这是一个正确的方法还是非常资源饥饿?

SOF的最近的问题是这一个,但它是在Python: extracting nearest stores

预先感谢您的建议。

回答

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