1

I have an array of CLLocation object. I would like to order this array with the distance to the user current location.

I know that I can calculate the distance between the user's location and each CLLocation object and then order it with this distance. Is there any other solution which doesn't need calculations? Thanks.

4

1 回答 1

4

不计算就无法得到距离。但计算相当简单,您只需在比较器块中执行一次。

[array sortUsingComparator:^NSComparisonResult(id o1, id o2) {
    CLLocation *l1 = o1, *l2 = o2;

    CLLocationDistance d1 = [l1 distanceFromLocation:userLocation];
    CLLocationDistance d2 = [l2 distanceFromLocation:userLocation];
    return d1 < d2 ? NSOrderedAscending : d1 > d2 ? NSOrderedDescending : NSOrderedSame;
}];
于 2013-06-17T20:59:41.787 回答