问题
I want to create a function that takes as parameters:
- An array filled of coordinates
- The current user location
And return the closest location between the user's location and the array locations.
Here are my locations:
let coord1 = CLLocation(latitude: 52.45678, longitude: 13.98765)
let coord2 = CLLocation(latitude: 52.12345, longitude: 13.54321)
let coord3 = CLLocation(latitude: 48.771896, longitude: 2.270748000000026)
User location function:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
var userLocation:CLLocation = locations[0]
let long = userLocation.coordinate.longitude;
let lat = userLocation.coordinate.latitude;
let coordinates = [ coord1, coord2, coord3]
userLocation = CLLocation(latitude: lat, longitude: long)
print("my location: \(userLocation)")
closestLocation(locations: coordinates, closestToLocation: userLocation)
}
Coordinates comparison function
func closestLocation(locations: [CLLocation], closestToLocation location: CLLocation) -> CLLocation? {
if let closestLocation: CLLocation = locations.min(by: { userLocation.distance(from: $0) < userLocation.distance(from: $1) }) {
let distanceMeters = userLocation.distance(from: closestLocation)
let distanceKM = distanceMeters / 1000
print("closest location: \(closestLocation), distance: \(distanceKM)")
return closestLocation
} else {
print("coordinates is empty")
return nil
}
}
It actually doesn't working and my closestLocation
function is always returning a huge distance between the two "closest locations".
Input parameters
closestLocation(locations: coordinates, closestToLocation: userLocation)
Edit
Result when I print closestLocation
and distanceKM
:
closest location: <+48.77189600,+2.27074800> +/- 0.00m (speed -1.00 mps / course -1.00) @ 14/01/2017 00:16:04 heure normale d’Europe centrale, distance: 5409.0
As you can see, the distance (in km) is very huge while these locations are the same city.
回答1:
You can use Array.min(by: )
to find the minimum element according to a sorting criteria (distance in your case):
func closestLocation(locations: [CLLocation], closestToLocation location: CLLocation) -> CLLocation? {
if let closestLocation = locations.min(by: { location.distance(from: $0) < location.distance(from: $1) }) {
print("closest location: \(closestLocation), distance: \(location.distance(from: closestLocation))")
return closestLocation
} else {
print("coordinates is empty")
return nil
}
}
// We know the answer is coord3 and the distance is 0
closestLocation(locations: [coord1, coord2, coord3], closestToLocation: coord3)
来源:https://stackoverflow.com/questions/41644250/find-closest-location-between-users-coordinates-and-coordinates-array