Reputation: 997
I am working on app in which i am first sending user Latitude and Longitude to the server . Server also has some previously stored coordinates, Now the app has to check if the user coordinate is within a radius of 1km or 2km of coordinates server already have.
One approach i found is of taking initial coordinate as circle center but it is not accurate. Any suggestion which algorithm i should follow and source which can help me understand this.
Upvotes: 0
Views: 1772
Reputation: 2019
R = 6378.1; // radius of the Earth (km)
dlat = deg2rad( $lat1-$lat2 ) // lattitude diference in radians
dlang = deg2rad ( $lang1 - $lang2 ) //longitude diference in radian
The Radius of the Earth is much bigger than 1-2 km , so we can use Pythagoras theorm, and we made 0.03 % accuracy ~ 0.3 m.
distance = R * sqrt( $dlat^2 + $dlang^2 );
// same: distance = sqrt( ($dlat*R)^2 + ($dlang*R)^2) )
This may help you to understand: http://en.wikipedia.org/wiki/Spherical_coordinate_system
Spherical coordinate system not the same as geolocation coordinate system, but to understand what you need, i think the below link has a good explain.
If you need more accurancy: See http://en.wikipedia.org/wiki/Great-circle_distance at Worked example section
When the distances only a few kilometers, the accuracy difference is small, but the computing time is much better when use the formula I wrote.
Upvotes: 2