要查找獲取的數據中最近的位置,您需要計算從當前位置到每個位置的距離。然後,只需對這些數據進行排序,就可以從現有的座標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];
}