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