Calculate angle between two Latitude/Longitude points

后端 未结 17 1411
故里飘歌
故里飘歌 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:27

    In case someone need PHP code for this functionality:

    /**
     * Calculate angle between 2 given latLng
     * @param  float $lat1
     * @param  float $lat2
     * @param  float $lng1
     * @param  float $lng2
     * @return integer
     */
    function angle($lat1, $lat2, $lng1, $lng2) {
        $dLon = $lng2 - $lng1;
        $y = sin($dLon) * cos($lat2);
        $x = cos($lat1) * sin($lat2) - sin($lat1) * cos($lat2) * cos($dLon);
        return 360 - ((rad2deg(atan2($y, $x)) + 360) % 360);
    }
    

提交回复
热议问题