Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Latitude / Longitude and meters

I have a small algorithmic problem.

I am developing an Android application. I get GPS coordinates. For example: latitude: 23.23907, longitude: 50.45786.

So I get a point. I want to compute bounds details on this point plus or minus 5 meters. I.e.:

[23.23907 - 5 meters ; 23.23907 + 5 meters]
[50.45786 - 5 meters ; 50.45786 + 5 meters]

How to make this calculation?

Thank you very much!

like image 831
Apaachee Avatar asked Mar 06 '13 21:03

Apaachee


1 Answers

The haversine formula can be simplified a great deal when you work in north-south and east-west directions only.

If Earth's circumference is C, the point at d kilometers to south of a given point is 360*d/C degrees to the south. The point at d kilometers to east is 360*d/(C*cos(latitude)) degrees to the east. The cosine in the denominator comes from the fact that the length of the longitude at a given latitude shorter than the equator by that much.

So if the Earth's circumference is 40075.04 km, to move 5 m to the north/south you would add/subtract 0.0000449 from the latitude and use the same longitude. To move 5 m to the west/east you would use the same latitude and add/subtract 0.0000449/cos(latitude) to the longitude. Don't forget about the edge cases though: near poles you have to clamp latitude to 90°, and near longitude 180° you'll have too add or subtract 360° to keep the longitude in the correct range.

With your numbers the range turns out to be approximately:

latitude:  [23.23903 ; 23.23911]
longitude: [50.45781 ; 50.45791]

Update: Note that this still assumes that the Earth is a perfect sphere, which it's not. The GPS system for example models the Earth as an ellipsoid where the equator is at 6378.137km and the poles are at 6356.7523142km from the center of the Earth. The difference is about 1/300th and matters a great deal for many applications, but in this case it's within the margin of error.

Correcting the formula for the longitude should be simple since the parallels are still circles: you would just have to swap cos(latitude) for the correct coefficient. Calculating the correct latitude is harder because the meridians are not circles but ellipses, and the arc length of an ellipse cannot be calculated using elementary functions, so you must use approximations.

like image 82
Joni Avatar answered Nov 15 '22 20:11

Joni