Calculate angle between two Latitude/Longitude points

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

    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

提交回复
热议问题