Monthly Archives: June 2016

How to rotate flight marker towards arrival airport in leaflet ?

 

flight_market_heading

First it is important to know how it works –

    1. Check out Leaflet Rotated Marker  plugin https://github.com/bbecquet/Leaflet.RotatedMarker
    2. L.marker([48.8631169, 2.3708919], {
          rotationAngle: 45
      }).addTo(map);
      
      

rotationAngle property need a value and this is key. You will have to generate that dynamically based on aircraft position and destination airport.  Function below does the same calculate  and give rotationAngle.

Function in Python

def computeHeading(lat1, long1, lat2, long2):
    import math
    
    rlat1 = math.radians(lat1)
    rlat2 = math.radians(lat2)
       
    dlong = math.radians(long2 - long1)
    
    y = math.cos(rlat2) * math.sin(dlong)
    x = math.cos(rlat1) * math.sin(rlat2) - math.sin(rlat1) * math.cos(rlat2) * math.cos(dlong)
    heading = round(math.degrees(math.atan2(y, x)) + 360, 4) % 360
    
    return heading

Keep in mind that lat1 and long1 are the aircraft’s position. And lat2, long2 are airport destination where aircraft is heading.

 

Function in PHP

function computeHeading($lat1, $long1, $lat2, $long2) // lat1 and long1 are the aircraft's position
{

    $rlat1 = deg2rad($lat1);
    //$rlat2 = radians($lat2);

    //$rlat1 = radians($lat1);
    $rlat2 = deg2rad($lat2);

    $dlong = deg2rad($long2 - $long1);
    //$dlong = radians(long2 - long1);

    $y = cos($rlat2) * sin($dlong);
    $x = cos($rlat1) * sin($rlat2) - sin($rlat1) * cos($rlat2) * cos($dlong);
    $heading = round(degrees(atan2($y, $x)) + 360, 4) % 360;

    return $heading;

}

Function in JavaScript

  function computeHeading(lat1, long1, lat2, long2)
    {

        // Converts from degrees to radians.
        Math.radians = function(degrees) {
            return degrees * Math.PI / 180;
        };

        // Converts from radians to degrees.
        Math.degrees = function(radians) {
            return radians * 180 / Math.PI;
        };


       var rlat1 = Math.radians(lat1);
       var rlat2 = Math.radians(lat2);

       var dlong = Math.radians(long2 - long1);

       var y = Math.cos(rlat2) * Math.sin(dlong);
       var x = Math.cos(rlat1) * Math.sin(rlat2) - Math.sin(rlat1) * Math.cos(rlat2) * Math.cos(dlong);
       var heading = Math.round(Math.degrees(Math.atan2(y, x)) + 360, 4) % 360;

        return heading;

    }
Share