3

我有一个 CLLocation 对象数组,我希望能够比较它们以获取与起始 CLLocation 对象的距离。数学很简单,但我很好奇是否有一个方便的排序描述符来做这件事?我应该避免 NSSortDescriptor 并编写自定义比较方法 + 冒泡排序吗?我通常最多比较 20 个对象,所以它不需要非常高效。

4

3 回答 3

14

您可以为 CLLocation 编写一个简单的 compareToLocation: category,根据 self 和其他 CLLocation 对象之间的距离返回 NSOrderedAscending、NSOrderedDescending 或 NSOrderedSame。然后简单地做这样的事情:

NSArray * mySortedDistances = [myDistancesArray sortedArrayUsingSelector:@selector(compareToLocation:)];

编辑:

像这样:

//CLLocation+DistanceComparison.h
static CLLocation * referenceLocation;
@interface CLLocation (DistanceComparison)
- (NSComparisonResult) compareToLocation:(CLLocation *)other;
@end

//CLLocation+DistanceComparison.m
@implementation CLLocation (DistanceComparison)
- (NSComparisonResult) compareToLocation:(CLLocation *)other {
  CLLocationDistance thisDistance = [self distanceFromLocation:referenceLocation];
  CLLocationDistance thatDistance = [other distanceFromLocation:referenceLocation];
  if (thisDistance < thatDistance) { return NSOrderedAscending; }
  if (thisDistance > thatDistance) { return NSOrderedDescending; }
  return NSOrderedSame;
}
@end


//somewhere else in your code
#import CLLocation+DistanceComparison.h
- (void) someMethod {
  //this is your array of CLLocations
  NSArray * distances = ...;
  referenceLocation = myStartingCLLocation;
  NSArray * mySortedDistances = [distances sortedArrayUsingSelector:@selector(compareToLocation:)];
  referenceLocation = nil;
}
于 2009-07-03T22:56:14.257 回答
2

为了改进戴夫的回答......

从 iOS 4 开始,您可以使用比较器块并避免使用静态变量和类别:

NSArray *sortedLocations = [self.locations sortedArrayUsingComparator:^NSComparisonResult(CLLocation *obj1, CLLocation *obj2) {
    CLLocationDistance distance1 = [targetLocation distanceFromLocation:loc1];
    CLLocationDistance distance2 = [targetLocation distanceFromLocation:loc2];

    if (distance1 < distance2)
    {
        return NSOrderedAscending;
    }
    else if (distance1 > distance2)
    {
        return NSOrderedDescending;
    }
    else
    {
        return NSOrderedSame;
    }
}];
于 2013-03-08T15:05:32.157 回答
1

只是添加到类别响应(这是要走的路),不要忘记您实际上不需要自己做任何数学运算,您可以使用 CLLocation 实例方法:

- (CLLocationDistance)getDistanceFrom:(const CLLocation *)location

获取两个位置对象之间的距离。

于 2009-07-04T00:16:42.723 回答