Posts

Post marked as solved
3 Replies
2.6k Views
Dears,How can I calculate the bearing between two lat and long?I'm not using the GPS, neither sensors, nor any other system... I'll use an internal database of points and its lat and long.The distance is working properly, but the bearing is not!Can you help me?//: Playground - noun: a place where people can play import CoreLocation import Foundation import UIKit let coordinate₀ = CLLocation(latitude: 0.0, longitude: 0.0) let coordinate₁ = CLLocation(latitude: -24.0, longitude: -47.0) let distanceInMeters = coordinate₀.distance(from: coordinate₁) // result is in meters public func bearing(to destination: CLLocation) -> Double { let lat1 = .pi * coordinate₀.coordinate.latitude / 180.0 let long1 = .pi * coordinate₀.coordinate.longitude / 180.0 let lat2 = .pi * coordinate₁.coordinate.latitude / 180.0 let long2 = .pi * coordinate₁.coordinate.longitude / 180.0 let rads = atan2( sin(long2 - long1) * cos(lat2), cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(long2 - long1)) let degrees = rads * 180 / Double.pi return (degrees+360).truncatingRemainder(dividingBy: 360) } let bearing = bearing(to: coordinate₁)
Posted Last updated
.