这里有一些可能对你有帮助的代码,(我花了很多时间让它工作,doc不是很有用)
它计算POI对象和currentLocation之间的距离,并从该位置开始最接近最远的POI对象
NSInteger compareDistance(id num1, id num2, void *context) { int retour; // fist we need to cast all the parameters CLLocation* location = context; POI* param1 = num1; POI* param2 = num2; // then we can use them as standard ObjC objects CLLocation* locationCoordinates = [[CLLocation alloc] initWithLatitude:location.coordinate.latitude longitude:location.coordinate.longitude]; CLLocation* locationPOI1 = [[CLLocation alloc] initWithLatitude:param1.coords.latitude longitude:param1.coords.longitude]; CLLocation* locationPOI2 = [[CLLocation alloc] initWithLatitude:param2.coords.latitude longitude:param2.coords.longitude]; CLLocationDistance distance1 = [locationPOI1 distanceFromLocation:locationCoordinates]; CLLocationDistance distance2 = [locationPOI2 distanceFromLocation:locationCoordinates]; //make the comparaison if (distance1 < distance2) retour = NSOrderedAscending; else if (distance1 > distance2) retour = NSOrderedDescending; else retour = NSOrderedSame; [locationCoordinates release]; [locationPOI1 release]; [locationPOI2 release]; return retour; } -(void) orderByProximityFromLocation:(CLLocationCoordinate2D) coordinates { CLLocation* currentLocation = [[CLLocation alloc] initWithLatitude:coordinates.latitude longitude:coordinates.longitude]; [listOfPOI sortUsingFunction:compareDistance context:currentLocation]; [currentLocation release]; }