Reputation: 1726
I am working on a GPS application and was toying with the idea of putting markers around the user like in Zombie Run and SpecTrek but am completely confused about how to find out the locations around the user.
I have been looking at the documentation for the Location class and have used the distanceTo() function for other things as well as MapView's latitudeSpan(), longitudeSpan() and getProjection() functions but I can't think how to decide on locations that are, for example, 100 metres around the user.
As I know the users position and I am only going to be placing markers that are ~1km away from the user, at the most, can I treat the area as flat rather than ellipsoidal and so then just could take the user's longitude and latitude and +/- from them to plot a marker around them (using some basic trigonometry such as x = cos(radius) and y = sin(radius) to keep it within the radius-sized circle around the player)?
I don't understand how long/lat correspond to actual scalar distances as in would 100long 100lat be 10 metres away from 90long 100lat? (I know these values are completely wrong but just using them to illustrate my question).
Thanks for your time,
Infinitifizz
Upvotes: 1
Views: 3878
Reputation: 2406
A little closer to the bottom of the page the formular is described a little bit better. There you can see that he converted to radians before calculating. Also it is crucial that you use the right datatypes to avoid false rounding of numbers. Here is a small code snippet which should work:
double lat1 = 52.104636;
double lon1 = 0.356324;
double R = 6371.0;
double d = 1.0;
double dist = d / R;
double brng = Math.toRadians(1.0);
lat1 = Math.toRadians(lat1);
lon1 = Math.toRadians(lon1);
double lat2 = Math.asin( Math.sin(lat1)*Math.cos(dist) + Math.cos(lat1)*Math.sin(dist)*Math.cos(brng));
double lon2 = lon1 + Math.atan2(Math.sin(brng)*Math.sin(dist)*Math.cos(lat1), Math.cos(dist)-Math.sin(lat1)*Math.sin(lat2));
lon2 = (lon2+3*Math.PI)%(2*Math.PI) - Math.PI;
System.out.println("lat2: " + Math.toDegrees(lat2));
System.out.println("lon2: " + Math.toDegrees(lon2));
Upvotes: 1
Reputation: 2406
The distance between two longitude/latitude points is calculated with the haversine formula. Here is a link with the theoretics: http://www.movable-type.co.uk/scripts/latlong.html
I would use the distanceTo method which you already mentioned for your purpose. You have your current Location and all your Points of Interest. Just call Location.distanceTo(Poi) for each Point of Interest and if the distance is larger than 1000 meters you can draw the point to your map.
If you don't have your PoIs as Location objects just build them like this:
poiLocation = new Location(LocationManager.PASSIVE_PROVIDER);
poiLocation.setLatitude(latitude);
poiLocation.setLongitude(longitude);
I used the distanceTo method in a radar like app and worked just fine.
Upvotes: 2