valid way to calculate angle between 2 CLLocations

2019-07-04 08:49发布

Is this a valid way to calculate an angle (in radians) from one CLLocation to another?

-(float)angleFromLocation:(CLLocationCoordinate2D)start toLocation:(CLLocationCoordinate2D)end {
float deltaX = start.latitude - end.latitude;
float deltaY = start.longitude - end.longitude;
float ang = atan2(deltaY, deltaX);

return ang;}

Please advise!

Any help will be much appreciated.

3条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-07-04 09:19

I used a variant of this question and answer and it works well:

double DegreesToRadians(double degrees) {return degrees * M_PI / 180.0;};
double RadiansToDegrees(double radians) {return radians * 180.0/M_PI;};

- (double)bearingFromLocation:(CLLocation *)fromLocation toLocation:(CLLocation *)toLocation
{

    double lat1 = DegreesToRadians(fromLocation.coordinate.latitude);
    double lon1 = DegreesToRadians(fromLocation.coordinate.longitude);

    double lat2 = DegreesToRadians(toLocation.coordinate.latitude);
    double lon2 = DegreesToRadians(toLocation.coordinate.longitude);

    double dLon = lon2 - lon1;

    double y = sin(dLon) * cos(lat2);
    double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);
    double radiansBearing = atan2(y, x);

    double degreesBearing = RadiansToDegrees(radiansBearing);

    if (degreesBearing >= 0) {
        return degreesBearing;
    } else {
        return degreesBearing + 360.0;
    }
}
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-07-04 09:26

Swift 4 version:

extension FloatingPoint {

    var degreesToRadians: Self { return self * .pi / 180 }
    var radiansToDegrees: Self { return self * 180 / .pi }
}

extension CLLocationCoordinate2D: Equatable {

    func heading(to: CLLocationCoordinate2D) -> Double {
        let lat1 = self.latitude.degreesToRadians
        let lon1 = self.longitude.degreesToRadians

        let lat2 = to.latitude.degreesToRadians
        let lon2 = to.longitude.degreesToRadians

        let dLon = lon2 - lon1
        let y = sin(dLon) * cos(lat2)
        let x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon)

        let headingDegrees = atan2(y, x).radiansToDegrees
        if headingDegrees >= 0 {
            return headingDegrees
        } else {
            return headingDegrees + 360
        }
    }
}
查看更多
我命由我不由天
4楼-- · 2019-07-04 09:29

The best method I found for this computation was to use the Spherical Law of Cosines. There is a C function to do this available here on github called headingInDegrees. It takes two lat/long pairs and returns heading:

/*------------------------------------------------------------------------- 
* Given two lat/lon points on earth, calculates the heading 
* from lat1/lon1 to lat2/lon2.   
*  
* lat/lon params in degrees 
* result in degrees 
*-------------------------------------------------------------------------*/
double headingInDegrees(double lat1, double lon1, double lat2, double lon2);

Since a CLLocationCoordinate2d contains latitude and longitude, it is easy to pass those two fields to this function and get the heading back.

查看更多
登录 后发表回答