Calculate angle between two Latitude/Longitude points

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

    With javascript, just use Turf:

    var point1 = turf.point([-75.343, 39.984]);
    var point2 = turf.point([-75.534, 39.123]);
    
    var bearing = turf.bearing(point1, point2);
    
    0 讨论(0)
  • 2020-12-07 18:27

    The general formula for calculating the angle(bearing) between two points is as follows:

    θ = atan2(sin(Δlong)*cos(lat2), cos(lat1)*sin(lat2) − sin(lat1)*cos(lat2)*cos(Δlong))
    

    Note that the angle(θ) should be converted to radians before using this formula and Δlong = long2 - long1.

    atan2 is a common function found in almost all programming languages (mostly in the Math package/library). Usually there are also functions for conversion between degrees and radians(also in the Math package/library).

    Remember that atan2 returns values in the range of -π ... +π, to convert the result to a compass bearing, you need to multiply θ by 180/π then use (θ+360) % 360, where % is modulus division operation returning the remainder of the division.

    The following link is a good resource for formulas involving latitudes and longitudes. They also provide Javascript implementation of their formulas. In fact, this answer is based on the information from this page:

    http://www.yourhomenow.com/house/haversine.html

    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
  • 2020-12-07 18:29

    Sample javascript code if the distance between points is less -

    brng = Math.atan2(newLat - oldLat, newLong - oldLong);
    brng = brng * (180 / Math.PI);
    brng = (brng + 360) % 360;
    brng = 360 - brng;
    
    0 讨论(0)
  • 2020-12-07 18:32

    To provide heading you have to compute bearing.

    To understand bearing read this article.

    According to this article (section bearing) the formula is :

    θ = atan2( sin Δλ ⋅ cos φ2 , cos φ1 ⋅ sin φ2 − sin φ1 ⋅ cos φ2 ⋅ cos Δλ )
    where φ1, λ1 is the start point, 
          φ2, λ2 the end point,
          Δλ is the difference in longitude`
    

    Here's a sample on how to compute the angle (in degrees) between two points expressed in Lat/Lon. (done in C#)

    Let's say Point is a simple class with two double attributes X (for longitude) and Y (for latitude).

    public double ComputeBearing(Point start,Point end)
    {
         var φ1 = start.Y; //latitude 1
         var λ1 = start.X; //longitude 1
         var φ2 = end.Y; //latitude 2
         var λ2 = end.X; //longitude 2
    
         var y = Math.Sin(this.degreeToRadian(λ2 - λ1)) * Math.Cos(this.degreeToRadian(φ2));
         var x = Math.Cos(this.degreeToRadian(φ1)) * Math.Sin(this.degreeToRadian(φ2)) - Math.Sin(this.degreeToRadian(φ1)) * Math.Cos(this.degreeToRadian(φ2)) * Math.Cos(this.degreeToRadian(λ2 - λ1));
    
         var θ = Math.Atan2(y, x);
         θ = this.radianToDegree(θ);
    
         return θ;
    }
    

    Using the following methods :

    public double degreeToRadian(double angle)
    {
        return Math.PI * angle / 180.0;
    }
    
    public double radianToDegree(double angle)
    {
        return angle * (180.0 / Math.PI);
    }
    

    By using ComputeBearing you will easily get an angle expressed in degrees easily usable as heading

    0 讨论(0)
  • 2020-12-07 18:32

    Maybe this is what you want:

    cos(say) = (cosd(90-lat(1))) * (cos(90-lat(2)))
             + (sin(90-lat(1))) * (sind(90-lat(2)) * (cosd(abs(Landa(2)-landa(1)))));
    
    0 讨论(0)
提交回复
热议问题