【问题标题】:Find nearest location from existing coordinates mapkit从现有坐标mapkit中查找最近的位置
【发布时间】:2011-12-01 12:22:03
【问题描述】:
我正在为拥有多家商店的客户开发 iphone 应用程序(目标 C)。我有一个数组中所有商店(20)的坐标(纬度,经度)。
目前我正在考虑遍历商店坐标数组并获取从用户当前位置到商店位置的距离,然后将它们添加到数组中并在最低距离上进行排序。
这是一种正确的方法还是非常消耗资源?
最近的 SOF 问题是这个问题,但它在 python 中:
extracting nearest stores
提前感谢您的建议。
【问题讨论】:
标签:
objective-c
ios
mapkit
【解决方案1】:
要在获取的数据中找到最近的位置,您需要计算从当前位置到您拥有的每个位置的距离。然后,只需对这些数据进行排序,您就会从现有的坐标 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];
}