0

I want to develop an android app which can calculate the actual distance traveled and show it in a text view. Here is the code I used but this code decreases the traveled distance when you heading back to the start point. Can anyone help to fix this code so it can accumulate distances only?

    display = (TextView) findViewById(R.id.info);

    LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE); 
    lm.requestLocationUpdates(lm.GPS_PROVIDER, 0,0, Loclist);
    loc = lm.getLastKnownLocation(lm.GPS_PROVIDER);

    if (loc == null) {
        display.setText("No GPS location found");
    } else {
        //set Current latitude and longitude
        currentLon = loc.getLongitude();
        currentLat = loc.getLatitude();
    }
    //Set the last latitude and longitude
    lastLat = currentLat;
    lastLon = currentLon;
}

LocationListener Loclist = new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        //set Current latitude and longitude
        currentLon=loc.getLongitude();
        currentLat=loc.getLatitude();
        // TODO Auto-generated method stub

        //start location manager
        LocationManager lm =(LocationManager) getSystemService(LOCATION_SERVICE);

        //Get last location
        Location loc = lm.getLastKnownLocation(lm.GPS_PROVIDER);

        //Request new location
        lm.requestLocationUpdates(lm.GPS_PROVIDER, 0,0, Loclist);

        //Get new location
        Location loc2 = lm.getLastKnownLocation(lm.GPS_PROVIDER);

        //get the current lat and long
        currentLat = loc.getLatitude();
        currentLon = loc.getLongitude();

        Location locationA = new Location("point A");
        locationA.setLatitude(lastLat);
        locationA.setLongitude(lastLon);

        Location locationB = new Location("point B");
        locationB.setLatitude(currentLat);
        locationB.setLongitude(currentLon);

        double distanceMeters = locationA.distanceTo(locationB);

        double distanceKm = distanceMeters / 1000f;

        display.setText(String.format("%.2f Km",distanceKm ));                  

        Location locationC = new Location("point c");
        locationA.setLatitude(currentLat);
        locationA.setLongitude(currentLon);
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

Thanks in advance.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
  • I'm so very confused. How do you make it accumulate distances only? Isn't that what you're doing already? – hichris123 Mar 21 '14 at 01:56
  • it's was no accumulating the distance, when you go back to the same way the distance were decreasing, but Libin has solved the problem. – user3166113 Mar 28 '14 at 23:20

1 Answers1

0

Follow these steps....

Step 1 :

   private  Location lastLocation;
   private  double totalDistance = 0;

Step 2 : In onCreate()

        //Get last location
        lastLocation = lm.getLastKnownLocation(lm.GPS_PROVIDER);

Step 2 : onLocationChanged(location) // calculate distance

        double distanceMeters = lastLocation.distanceTo(location);
        double distanceKm = distanceMeters / 1000f;
        // when the location change keep updating the total distance
        totalDistance += distanceKm;
        lastLocation = location; // assign the current location to lastLocation.
Libin
  • 16,967
  • 7
  • 61
  • 83