5

In der Frage here geschrieben, der Benutzer gefragt:Suche nächste Längen- und Breitengrad in Array von Benutzerposition - iOS Swift

I voller Länge und Breite ein Array haben. Ich habe zwei doppelte Variablen mit dem Standort meines Benutzers. Ich möchte den Abstand zwischen den Standorten meines Benutzers mit meinem Array vergleichen, um zu sehen, welcher Standort am nächsten ist. Wie mache ich das?

Dies wird die Entfernung zwischen 2 Ort aber Stuggeling erhalten, um zu verstehen, wie ich es gegen eine Reihe von Standorten testen würde.

Als Antwort bekam er den folgenden Code:

NSArray *locations = //your array of CLLocation objects 
CLLocation *currentLocation = //current device Location 

CLLocation *closestLocation; 
CLLocationDistance smallestDistance = DBL_MAX; // set the max value 

for (CLLocation *location in locations) { 
    CLLocationDistance distance = [currentLocation distanceFromLocation:location]; 

    if (distance < smallestDistance) { 
     smallestDistance = distance; 
     closestLocation = location; 
    } 
} 
NSLog(@"smallestDistance = %f", smallestDistance); 

ich genau das gleiche Problem in einer Anwendung haben auf ich arbeite, und ich denke, dass dieses Stück Code könnte perfekt funktionieren. Allerdings verwende ich Swift, und dieser Code ist in Objective-C.

Meine einzige Frage ist: Wie sollte es in Swift aussehen?

Danke für jede Hilfe. Ich bin neu in all dem, und dieses Stück Code in Swift zu sehen, könnte ein großes Bein sein.

Antwort

16
var closestLocation: CLLocation? 
var smallestDistance: CLLocationDistance? 

for location in locations { 
    let distance = currentLocation.distanceFromLocation(location) 
    if smallestDistance == nil || distance < smallestDistance { 
    closestLocation = location 
    smallestDistance = distance 
    } 
} 

print("smallestDistance = \(smallestDistance)") 

oder als Funktion:

func locationInLocations(locations: [CLLocation], closestToLocation location: CLLocation) -> CLLocation? { 
    if locations.count == 0 { 
    return nil 
    } 

    var closestLocation: CLLocation? 
    var smallestDistance: CLLocationDistance? 

    for location in locations { 
    let distance = location.distanceFromLocation(location) 
    if smallestDistance == nil || distance < smallestDistance { 
     closestLocation = location 
     smallestDistance = distance 
    } 
    } 

    print("closestLocation: \(closestLocation), distance: \(smallestDistance)") 
    return closestLocation 
} 
+0

Jetzt kann ich dies mit vergleichen verwenden Ziel c. Es ist in mehr als einer Hinsicht hilfreich. Vielen Dank! – tsteve

+0

froh, ich könnte helfen! –

14

Für Swift 3 ich dieses kleine Stück "funktional" Code erstellt haben:

let coord1 = CLLocation(latitude: 52.12345, longitude: 13.54321) 
let coord2 = CLLocation(latitude: 52.45678, longitude: 13.98765) 
let coord3 = CLLocation(latitude: 53.45678, longitude: 13.54455) 

let coordinates = [coord1, coord2, coord3] 

let userLocation = CLLocation(latitude: 52.23678, longitude: 13.55555) 

let closest = coordinates.min(by: 
{ $0.distance(from: userLocation) < $1.distance(from: userLocation) })