So, lets say your application needs to let the user know when he/she is within, say, 1 mile of a given location. Or, you want to sort a series of locations based on how close to the user the points are in a straight line. How do you go about doing that? One way would be to use the Haversine formula to figure out the distances between two points, considering that Earth is spherical, but that takes math, and who likes doing that?

Thankfully, there is a much simpler way to figure out distances between two points on a map: the distanceTo() method in the Location class. Using this handy little method, we can quickly find out the distance between two locations:

double distance;

Location locationA = new Location("point A");

locationA.setLatitude(latA);
locationA.setLongitude(lngA);

Location locationB = new Location("point B");

locationB.setLatitude(latB);
LocationB.setLongitude(lngB);

distance = locationA.distanceTo(locationB);

One thing to note is that the distanceTo() method returns the distance in meters, so you will need to do the appropriate calculations if you want your distances in other units.