3

Possible Duplicate:
How to track distance via GPS on Android?

I have designed a GPS application and its telling my location well. But Now I want to include more feature. How I will make a radius there? to have a surrounding area of 5 or 6 km! How I can mention the distance between a place on that area and my place?

Community
  • 1
  • 1
Saad
  • 45
  • 3
  • 2
    there are many ways to draw a map in android.. and with each way there is a different way to get the distance between two points.. you need to provide more info on how you are creating the map. – JustinMorris Nov 01 '12 at 16:11
  • Also specify if you are trying to calculate the distance between the user and all points within the radius or just from user to a single specific location. How are you storing all of these coordinates, and finally what have you tried? – jnthnjns Nov 01 '12 at 16:15
  • I am using google api for my location tracking. I want to calculate the distance between the user and a specific place. I am not sure about storing data. How I can make this possible that I can store few data(latitude, longitude) with in the radius and find the distance from my place to those place.I am sharing my main coding part here: – Saad Nov 01 '12 at 16:23
  • I think coding part is too heavy for this place. I used overlay in my map and used google apis for mapping. How I can have the distance? Please mention if you need to know any other information. – Saad Nov 01 '12 at 16:26

2 Answers2

2

If you simply have different coordinates and want to do calculations with them, just check out the Android functions already available for it: http://developer.android.com/reference/android/location/Location.html

You can create Location objects, put lat/long coordinates with the set-functions and then just use

float distanceInMeters=location1.distanceTo(location2);

to get results.

Oliver
  • 1,269
  • 13
  • 21
0

I feel like this question is starting to turn into a lot of questions. I decided to tackle this answer by directing it towards your question title "Distance for GPS Application".

In my application, instead of using Google's API's I request the users distance from a list of GPS coordinates by doing the following:

In my JJMath Class:

Getting distance (Haversine Formula, in miles):

/**
 * @param lat1
 * Latitude which was given by the device's internal GPS or Network location provider of the users location
 * @param lng1
 * Longitude which was given by the device's internal GPS or Network location provider of the users location 
 * @param lat2
 * Latitude of the object in which the user wants to know the distance they are from
 * @param lng2
 * Longitude of the object in which the user wants to know the distance they are from
 * @return
 * Distance from which the user is located from the specified target
*/
public static double distFrom(double lat1, double lng1, double lat2, double lng2) {
    double earthRadius = 3958.75;
    double dLat = Math.toRadians(lat2-lat1);
    double dLng = Math.toRadians(lng2-lng1);
    double sindLat = Math.sin(dLat / 2);
    double sindLng = Math.sin(dLng / 2);
    double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2) * Math.cos(lat1) * Math.cos(lat2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    double dist = earthRadius * c;

    return dist;
}

Then I round that number by:

/** This gives me numeric value to the tenth (i.e. 6.1) */
public static double round(double unrounded) {
    BigDecimal bd = new BigDecimal(unrounded);
    BigDecimal rounded = bd.setScale(1, BigDecimal.ROUND_CEILING);
    return rounded.doubleValue();
}

I don't work with Map overlays, but I am sure there are great tutorials or answers to come.

jnthnjns
  • 8,962
  • 4
  • 42
  • 65