Calculate angle between two Latitude/Longitude points

后端 未结 17 1400
故里飘歌
故里飘歌 2020-12-07 17:51

Is there a way to calculate angle between two Latitude/Longitude points?

What I am trying to achieve is to know where the user is heading. For example, user is head

17条回答
  •  佛祖请我去吃肉
    2020-12-07 18:14

    Considering Nayanesh Gupte's answer and its comments. I've changed some part of the code and wrote it in PHP.

    • latitude and longitude have been converted to radians inside the function.

    Here is the function:

    function angleFromCoordinate($lat1, $long1, $lat2, $long2) {
    
        $lat1 = deg2rad($lat1);
        $lat2 = deg2rad($lat2);
        $long1 = deg2rad($long1);
        $long2 = deg2rad($long2);
    
        $dLon = $long2 - $long1;
    
        $y = sin($dLon) * cos($lat2);
        $x = cos($lat1) * sin($lat2) - sin($lat1) * cos($lat2) * cos($dLon);
    
        $brng = atan2($y, $x);
    
        $brng = $brng * 180 / pi();
        $brng = fmod($brng + 360, 360);
    
        return $brng;
    }
    

提交回复
热议问题