1

I'm building an application that tracks my running and walking but I'm getting some trouble with calculating the total distance.

Here is a part of my code where I'm getting the values of latitude and longitude and where I do my calculation of the distance.

public class SportsActicity {

protected Integer userId;
protected ArrayList<Position> route = new ArrayList<Position>();

public SportsActicity() {
    this.userId = 1;
}

public void track(Double latitude, Double longitude, Double altitude, float speed, long chron) {
    Date currentDate = new Date();
    Position currentPosition = new Position(currentDate, latitude, longitude, altitude, speed, chron);
    route.add(currentPosition);
    // Float velocidade =  (locat.getSpeed()*3600)/1000;
}

public double getTotalDistance() {
    double distance = 0;
    if(route.size() > 1) {
        for (Integer i = 0; i < route.size() - 1; i++) {
            distance += test(route.get(i).latitude, route.get(i).longitude, route.get(i + 1).latitude, route.get(i + 1).longitude);
        }
    }
    return distance;
}

public Float test(Double lat1, Double lon1, Double lat2, Double lon2) {
    double earthRadius = 6371;
    double latDiff = Math.toRadians(lat2-lat1);
    double lngDiff = Math.toRadians(lon2-lon1);
    double a = Math.sin(latDiff /2) * Math.sin(latDiff /2) +           
               Math.cos(Math.toRadians(lat1))* 
               Math.cos(Math.toRadians(lat2))* Math.sin(lngDiff /2) * 
               Math.sin(lngDiff /2); 
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double distance = earthRadius * c;

    int meterConversion = 1609;

    return new Float(distance * meterConversion).floatValue();
}

enter image description here

If I'm not moving, the total distance is always incrementing, around 10 (no idea if it is in meters), and the values I'm getting don't look correct.

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
Jose Rafael
  • 11
  • 1
  • 3

2 Answers2

1

You can use Location's static method distanceBetween instead of your whole test method.

int[] results = new int[1];
Location.distanceBetween(lat1, lon1, lat2, lon2, results);
int distance = results[0];
Bartek Lipinski
  • 30,698
  • 10
  • 94
  • 132
  • http://i.stack.imgur.com/ssjIY.png I did what u said but I still get "stupid" values with the phone standing at the window. The values in distance was 0-8-11, and the coordinates are the 3 that u can see in the image. Regards – Jose Rafael Nov 24 '15 at 22:36
0

From Google official repository link

SphericalUtil

MathUtil

Usage

 double distance = SphericalUtil.computeDistanceBetween(new LatLng(9.000,10.00), new LatLng(9.000,11.00));

The above method will returns the distance between two LatLngs, in meters. Or try this

private String getDistance(LatLng my_latlong,LatLng frnd_latlong){
 Location l1=new Location("One");
 l1.setLatitude(my_latlong.latitude);
 l1.setLongitude(my_latlong.longitude);

 Location l2=new Location("Two");
 l2.setLatitude(frnd_latlong.latitude);
 l2.setLongitude(frnd_latlong.longitude);

 float distance=l1.distanceTo(l2);
 String dist=distance+" M";

  if(distance>1000.0f)
   {
     distance=distance/1000.0f;
     dist=distance+" KM";
   }

 return dist;

}

or you can give a look at link

Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73
  • Thank you. I just forgot that GPS has an error. I just took the distance will I was driving and it hit right with the car's value (100m error). – Jose Rafael Nov 25 '15 at 19:45