Calculating bearing between two CLLocationCoordinate2Ds

前端 未结 4 1664
遇见更好的自我
遇见更好的自我 2020-11-27 13:07

Very \"simple\" problem: given two CLLocationCoordinate2Ds, how can I get the bearing (as radians) from the first to the second? I\'ve done a lot of research and studying on

4条回答
  •  攒了一身酷
    2020-11-27 13:53

    you can use my code.. it's work on my project with microcontroller that use GPS for data.

    #define d2r ((22/7.0)/180.0)
    #define r2d (180.0/(22/7.0))
    
    double get_heading1(double lat1, double long1, double lat2, double long2)  
    {
        double diff_lat, diff_long;
        double degree;
    
        diff_long =(double) (((long2*1000000)-(long1*1000000))/1000000) * d2r;
        diff_lat = (double) (((lat2*1000000)-(lat1*1000000))/1000000) * d2r;     
    
        degree = r2d     (atan2(sin(diff_long)*cos(d2r*lat2),cos(d2r*lat1)*sin(d2r*lat2)-sin(d2r*lat1)*cos(d2r*lat2)    *cos(diff_long)));
    
        if (degree >= 0) {
            return degree;
        } else {
            return 360+degree;
        }                                                                 
    }
    

提交回复
热议问题