I want to make an app which checks the nearest place where a user is. I can easily get the location of the user and I have already a list of places with latitude and longitude.
What would be the best way to know the nearest place of the list against the current user location.
I could not find anything in the google APIs.
Location loc1 = new Location("");
loc1.setLatitude(lat1);
loc1.setLongitude(lon1);
Location loc2 = new Location("");
loc2.setLatitude(lat2);
loc2.setLongitude(lon2);
float distanceInMeters = loc1.distanceTo(loc2);
Reference: http://developer.android.com/reference/android/location/Location.html#distanceTo(android.location.Location)
http://developer.android.com/reference/android/location/Location.html
Look into distanceTo or distanceBetween. You can create a Location object from a latitude and longitude:
Location location = new Location("");
location.setLatitude(lat);
location.setLongitude(lon);
An approximated solution (based on an equirectangular projection), much faster (it requires only 1 trig and 1 square root).
This approximation is relevant if your points are not too far apart. It will always over-estimate compared to the real haversine distance. For example it will add no more than 0.05382 % to the real distance if the delta latitude or longitude between your two points does not exceed 4 decimal degrees.
The standard formula (Haversine) is the exact one (that is, it works for any couple of longitude/latitude on earth) but is much slower as it needs 7 trigonometric and 2 square roots. If your couple of points are not too far apart, and absolute precision is not paramount, you can use this approximate version (Equirectangular), which is much faster as it uses only one trigonometric and one square root.
// Approximate Equirectangular -- works if (lat1,lon1) ~ (lat2,lon2)
int R = 6371; // km
double x = (lon2 - lon1) * Math.cos((lat1 + lat2) / 2);
double y = (lat2 - lat1);
double distance = Math.sqrt(x * x + y * y) * R;
You can optimize this further by either:
For more info see: http://www.movable-type.co.uk/scripts/latlong.html
There is a nice reference implementation of the Haversine formula in several languages at: http://www.codecodex.com/wiki/Calculate_Distance_Between_Two_Points_on_a_Globe
There are a couple of methods you could use, but to determine which one is best we first need to know if you are aware of the user's altitude, as well as the altitude of the other points?
Depending on the level of accuracy you are after, you could look into either the Haversine or Vincenty formulae...
These pages detail the formulae, and, for the less mathematically inclined also provide an explanation of how to implement them in script!
Haversine Formula: http://www.movable-type.co.uk/scripts/latlong.html
Vincenty Formula: http://www.movable-type.co.uk/scripts/latlong-vincenty.html
If you have any problems with any of the meanings in the formulae, just comment and I'll do my best to answer them :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With