I know from documentation we can find distance between two CLLocation points using the function, distanceFromLocation:. But my problem is I dont ha
Swift 5: CLLocationCoordinate2D extension to calculate distance:
extension CLLocationCoordinate2D {
/// Returns distance from coordianate in meters.
/// - Parameter from: coordinate which will be used as end point.
/// - Returns: Returns distance in meters.
func distance(from: CLLocationCoordinate2D) -> CLLocationDistance {
let from = CLLocation(latitude: from.latitude, longitude: from.longitude)
let to = CLLocation(latitude: self.latitude, longitude: self.longitude)
return from.distance(from: to)
}
}
Usage:
let coordinate = CLLocationCoordinate2D(latitude: 11, longitude: 39)
let from = CLLocationCoordinate2D(latitude: 30, longitude: 43)
let distance = coordinate.distance(from: from)