Calculating bearing between two CLLocation points in Swift
Here is an Objective-C solution
- CLLocation Category for Calculating Bearing w/ Haversine function
which can easily be translated to Swift:
func degreesToRadians(degrees: Double) -> Double { return degrees * .pi / 180.0 }
func radiansToDegrees(radians: Double) -> Double { return radians * 180.0 / .pi }
func getBearingBetweenTwoPoints1(point1 : CLLocation, point2 : CLLocation) -> Double {
let lat1 = degreesToRadians(degrees: point1.coordinate.latitude)
let lon1 = degreesToRadians(degrees: point1.coordinate.longitude)
let lat2 = degreesToRadians(degrees: point2.coordinate.latitude)
let lon2 = degreesToRadians(degrees: point2.coordinate.longitude)
let dLon = lon2 - lon1
let y = sin(dLon) * cos(lat2)
let x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon)
let radiansBearing = atan2(y, x)
return radiansToDegrees(radians: radiansBearing)
}
The result type is Double
because that is how all location coordinates are
stored (CLLocationDegrees
is a type alias for Double
).
Calculating bearing between two CLLocationCoordinate2Ds
Your math is correct, with the following exceptions:
Make sure to convert fLat, fLon, tLat, and tLon to radians before applying any sin() or cos() to them. Divide by 180.0 and multiply by PI.
Enter the delta between tLng and fLng as tLng-fLng, and not the other way around. Note that this difference appears twice in the expression.
With those changes, I am getting 1.18660677830947 radians with double precision math and the values in the question.
CLLocation Category for Calculating Bearing w/ Haversine function
Your code seems fine to me. Nothing wrong with the calculous. You don't specify how far off your results are, but you might try tweaking your radian/degrees converters to this:
double DegreesToRadians(double degrees) {return degrees * M_PI / 180.0;};
double RadiansToDegrees(double radians) {return radians * 180.0/M_PI;};
If you are getting negative bearings, add 2*M_PI
to the final result in radiansBearing (or 360 if you do it after converting to degrees). atan2 returns the result in the range -M_PI
to M_PI
(-180 to 180 degrees), so you might want to convert it to compass bearings, using something like the following code
if(radiansBearing < 0.0)
radiansBearing += 2*M_PI;
Bearing of Current location in iOS SDK
CLLocation
has a course
property (type CLLocationDirection
) which is the bearing. From the docs:
Course values are measured in degrees starting at due north and continuing clockwise around the compass. Thus, north is 0 degrees, east is 90 degrees, south is 180 degrees, and so on. Course values may not be available on all devices. A negative value indicates that the direction is invalid.
How to find out distance between coordinates?
CLLocation has a distanceFromLocation method so given two CLLocations:
CLLocationDistance distanceInMeters = [location1 distanceFromLocation:location2];
or in Swift 4:
//: Playground - noun: a place where people can play
import CoreLocation
let coordinate₀ = CLLocation(latitude: 5.0, longitude: 5.0)
let coordinate₁ = CLLocation(latitude: 5.0, longitude: 3.0)
let distanceInMeters = coordinate₀.distance(from: coordinate₁) // result is in meters
you get here distance in meter so 1 miles = 1609 meter
if(distanceInMeters <= 1609)
{
// under 1 mile
}
else
{
// out of 1 mile
}
Calculate offset between two CLLocationCoordinate2D locations
If I understand, what you're trying to get is the distance between 2 known location in degrees ?
If this is it then try :
class func getDistancesInDegrees(origin:CLLocationCoordinate2D, destination:CLLocationCoordinate2D) -> (degLat: CLLocationDegrees, degLon:CLLocationDegrees) {
var latidueDegrees:CLLocationDegrees = Double(origin.coordinate.latitude) - Double(destination.coordinate.latitude)
var longitudeDegrees:CLLocationDegrees = Double(origin.coordinate.longitude) - Double(destination.coordinate.longitude)
return (degLat: latidueDegrees, degLon:longitudeDegrees)
}
Related Topics
How to Disable Uitextfield Editing But Still Accept Touch
Auto-Layout: What Creates Constraints Named Uiview-Encapsulated-Layout-Width & Height
How to Remove Cache in Wkwebview
How to Check Internet Connection in Alamofire
Swift iOS Check If Remote Push Notifications Are Enabled in iOS9 and iOS10
How to Get Uiscrollview Vertical Direction in Swift
Prevent Dismissal of Modal View Controller in Swiftui
Single Function to Dismiss All Open View Controllers
Change Language of Alert in Banner of Push Notification
Kvo and Arc How to Removeobserver
Xcode 10: Unable to Attach Db Error
Why am I Getting Ibtool Failed with Exit Code 255
Making a Phone Call in an iOS Application
Auto Layout Uiscrollview with Subviews with Dynamic Heights
iPhone Is Not Available. Please Reconnect the Device
Hide Keyboard for Text Field in Swift Programming Language
Xcode 6 Keeps Renaming My App's Directory in iOS8 Simulator After Each Run