I have a CLLocation defined, and I\'d like to move that point x meters to the east and y meters to the south. How may I achieve that?
Swift implementation using Measurement struct to do the conversions between degrees and radians.
class GPSLocation {
public class func degreesToRadians(degrees: Double) -> Double {
return Measurement(value: degrees, unit: UnitAngle.degrees).converted(to: .radians).value
}
public class func radiansToDegrees(radians: Double) -> Double {
return Measurement(value: radians, unit: UnitAngle.radians).converted(to: .degrees).value
}
public class func location(location: CLLocation, byMovingDistance distance: Double, withBearing bearingDegrees:CLLocationDirection) -> CLLocation {
let distanceRadians: Double = distance / 6372797.6
let bearingRadians: Double = GPSLocation.degreesToRadians(degrees: bearingDegrees)
let lat1 = GPSLocation.degreesToRadians(degrees: location.coordinate.latitude)
let lon1 = GPSLocation.degreesToRadians(degrees: location.coordinate.longitude)
let lat2 = GPSLocation.radiansToDegrees(radians:asin(sin(lat1) * cos(distanceRadians) + cos(lat1) * sin(distanceRadians) * cos(bearingRadians)))
let lon2 = GPSLocation.radiansToDegrees(radians:lon1 + atan2(sin(bearingRadians) * sin(distanceRadians * cos(lat1)), cos(distanceRadians) - sin(lat1) * sin(lat2)))
return CLLocation(latitude: lat2, longitude: lon2)
}
}