Frage

Ich habe ein Array von CLLocation Objekten und ich möchte sie vergleichen können Entfernung von einem Start CLLocation Objekt zu erhalten. Die Mathematik ist gerade nach vorne, aber ich bin neugierig, ob es eine Bequemlichkeit Art Descriptor über das tun dies gehen soll? Sollte ich NSSortDescriptor vermeiden und eine benutzerdefinierte vergleichen Methode + Blasensortierung schreiben? Ich bin in der Regel höchstens 20 Objekte zu vergleichen, so muss es nicht sehr effizient sein.

War es hilfreich?

Lösung

Kategorie für CLLocation, dass die Renditen entweder NSOrderedAscending, NSOrderedDescending oder NSOrderedSame zwischen dem Selbst und der anderen CLLocation Objekt auf die Abstände abhängig:

Sie können einen einfachen compareToLocation schreiben. Dann einfach etwas tun, wie folgt aus:

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

Edit:

Wie folgt aus:

//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;
}

Andere Tipps

Um auf Daves Antwort zu verbessern ...

Ab iOS 4 können Sie einen Vergleichsblock und vermeiden Sie eine statische Variable und Kategorie verwenden:

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;
    }
}];

Sie einfach auf die Kategorie Antwort hinzuzufügen (das ist der Weg zu gehen), vergessen Sie nicht, dass Sie tatsächlich noch keine Mathe selbst tun müssen, können Sie die CLLocation Instanz-Methode verwenden:

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

Um den Abstand zwischen zwei Ortsobjekten zu erhalten.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top