问题
I have an App with a TextView
that finds your location and when you move, the value in TextView
changes.
I want to know what can I do in order to register the first location and then look for the location 5 min after.
For example:
min 0
LAT 000000
LONG 000000
( SAVE IT )
min 5
LAT 1111111
LONG 111111
if min0 == min 5
...
To sum up, I want to compare 2 strings (lat & long at the beginning) and lat & long after 5 minutes.
Any suggestion will be appreciated.
回答1:
This Link should serve your purpose;
Go through distanceTo or distanceBetween
. You can create a Location object from a latitude and longitude:
Location location = new Location("");
location.setLatitude(lat);
location.setLongitude(lon);
Another Solution I came across,
There is a standard formula (Haversine) (that is, it works for any couple of longitude/latitude on earth) but the performance is slow as it needs 7 trigonometric and 2 square roots.
If your couple of points are not too far apart, you can use an approximate
version (Equirectangular), which is much faster.
// Approximate Equirectangular -- works if (lat1,lon1) ~ (lat2,lon2)
double x = (lon2 - lon1) * Math.cos((lat1 + lat2) / 2);
double y = (lat2 - lat1);
double d = Math.sqrt(x * x + y * y) * R; // where R is the earth's Radius
// R is 6,371 km
Hope that helps.
回答2:
To compare the two strings, use String.equals(other String), http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#equals(java.lang.Object) but the locations and string formatting would have to be identical.
If you wanted to put some kind of buffer on it, so the location doesn't have to be exactly the same, you could parse the string and save the Lat and Long into an integer, then compare it with your delta before triggering your textview update.
来源:https://stackoverflow.com/questions/18146848/how-to-compare-2-location-positions