Issue with calculating compass bearing between two GPS coordinates

前端 未结 3 1518
挽巷
挽巷 2021-02-04 15:35

In my webapp, have a JSON data response from a database query that includes the lat/long coordinates of 1 to n locations. I want to calculate the bearing from the data[i]

3条回答
  •  情话喂你
    2021-02-04 16:18

    If you want a very rough method for short distances, you can use an Earth radius of 6,378,137m (the length of the semi-major axis of the WGS84 spheroid) to calculate the sides of the triangle based on the difference in latitude and longitude. Then calculate the appropriate bearing. It will be a true bearing, but likely close enough over short distances.

    You'll need to leave it up to users to work out the local magnetic declination.

    e.g. for your example:

    startLat  = 43.6822
    startLong = -70.450769
    
    endLat  = 43.682211
    endLong = -70.45070
    
    diff lat  = 0.000011 = 1.22m
    diff long = 0.000069 = 7.68m
    

    The end point is north and east of the start, so the bearing can be found by:

    tan a = 7.68 / 1.22
        a = 81°
    

    So the direction is about East by North.

    This should probably be in a mapping and surveying thread. Once you've got the maths worked out, come here for the solution.

    Edit

    To convert degrees of latitude to metres, first calculate the Earth circumference at the equator (or any great circle):

    c = 2πR where r = 6378137m
      = 40,075,000 (approx)
    

    Then get the ratio of the circumference out of 360°:

    dist = c * deg / 360
         = 40,075,000m * 0.000011° / 360°
         = 1.223m
    

    For longitude, the distance narrows as the latitude approaches the pole, so the same formula is used and the result multiplied by the cosine of the latitude:

         = 40,075,000m * 0.000069° / 360° * cos(0.000011°)
         = 7.681m
    

    The value for the Earth radius is not necessarily accurate, the Earth isn't a perfect sphere (it's an oblate spheroid, sort of pear shaped). Different approximations are used in different places for greater accuracy, but the one I've used should be good enough.

提交回复
热议问题