Calculate angle between two Latitude/Longitude points

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

    Based on Nayanesh Gupte's answer, here is a Python implementation of how to calculate the angle between two points defined by their latitudes and longitudes if anyone needs it:

    def angleFromCoordinate(lat1, long1, lat2, long2):
        dLon = (long2 - long1)
    
        y = math.sin(dLon) * math.cos(lat2)
        x = math.cos(lat1) * math.sin(lat2) - math.sin(lat1) * math.cos(lat2) * math.cos(dLon)
    
        brng = math.atan2(y, x)
    
        brng = math.degrees(brng)
        brng = (brng + 360) % 360
        brng = 360 - brng # count degrees clockwise - remove to make counter-clockwise
    
        return brng
    

    Where an angle of 0 degrees indicates a northward heading.

提交回复
热议问题