Swift 좌표간의 거리 구하기

2021. 4. 23. 18:09아이폰 개발

CLLocationCoordinate2D에 저장된 좌표를 이용하여 CLLocation 객체로 생성하여 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)
    }
}

stackoverflow.com/questions/11077425/finding-distance-between-cllocationcoordinate2d-points

 

Finding distance between CLLocationCoordinate2D points

I know from documentation we can find distance between two CLLocation points using the function, distanceFromLocation:. But my problem is I dont have CLLocation data type with me, I have the

stackoverflow.com