I have location from GPS (lon_base, lat_base). I have a list of locations (lon1, lat1|lon2, lat2|lon3, lat3...) This list is very long and is around the world.
My questions are: 1. How do I get from that list only the lon\lat that are 1 mile from my lon_base\lat_base? 2. How do I sort them from closest to farthest?
Thanks in advance!
You want to define your own
Comparator
that, in general, looks something like this:where
calcDistance()
simply calculates the distance between the two points. If you're on Android, I think Google Maps has a function somewhere in their API that will do this for you.EDIT : You'll want your
calcDistance()
function to look like ChrisJ'sdistance
function.-tjw
Where the List of Locations is a list containing your own Location class, not the android.location.Location.
You can use followig approximation (since 1 mile is much smaller than the radius of the earth) to calculate the distances from your base:
with:
phi
= latitude andtheta
= longitudeThe result is in units of 60 nautical miles if
theta
andphi
are given in degrees. The results will be quite wrong for points that have a latitude that is much different from your base latitude, but this doesn't matter if you just want to know wich points are about 1 mile from your base.For most programming languages you have to convert
phi_base
to radians (multiply by pi/180) in order to use it forcos()
.(Attention: You have to take special care if your base longitude is very close to 180° or -180°, but probably that is not the case :-)
Use the calculated distances as sorting key to sort your points.
If you have to be more exact (e.g. if you want to know all points that are about 2000 miles from your home), than you must use the formula for Great Circle Distance to calculate the exact distance of two points on a sphere.
You may use the great circle distance to calculate the distance between two points whose you know the latitude-longitude coordinates. The formulae are quite easy to code:
According to this link i made working method. The answer above was wrong, because it doesn't convert lat/lng degrees to radians.