Sarah Jane
Sarah Jane

Reputation: 3

Calculating distance traveled in Android/Google Maps

I'm working on a tracking system and it involves tracking a users path. I'm having trouble calculating the distance. I've been using the start location along with the most recent location update but that does not calculate the actual distance. In the onLocationChanged method I pass the location to my updateDistance function. I understand that the route needs to be broken into segments and then each segments value added to the total but I can't solve the problem.

I've searched the site and am aware there is some similar topics but I haven't found a solution to my problem so I would be appreciative of any help. Here is my function to calculate distance so far.

private void updateDistance(Location location) {
    Log.i("updateDistance", "In distance function");
    locationA = new Location("point A");
    locationB = new Location("point B");

    locationA.setLatitude(firstlat);
    locationA.setLongitude(firstlon);

    lat = location.getLatitude();
    lon = location.getLongitude();

    locationB.setLatitude(lat);
    locationB.setLongitude(lon);
    // Calculate the distance between them
    distance = locationA.distanceTo(locationB);
    // if less than 10 metres, do not record
    if (distance < 10.00) {
        Log.i("DISTANCE", "Values too close, so not used.");
    } else
    tv1.setText("Distance Travelled: " + distanceTravelled + " metres");
}

Upvotes: 0

Views: 2974

Answers (2)

Riddhi Dudani
Riddhi Dudani

Reputation: 107

I was too confused at this point but finally I got a solution.

Here is the solution.

store the value of location in another variable in onCreate method (it is important)

location = locationManager.getLastKnownLocation(provider);
oldLocation=location;

and then update it every time when onLocationChanged is called.

    @Override
    public void onLocationChanged(Location location) {

            if (location != null) {

                // locationManager.removeUpdates(mylistener);
                lat_new = location.getLatitude();
                lon_new = location.getLongitude();

                float distance = oldLocation.distanceTo(location);
                // speed = distance / time;

                distanceCovered = distanceCovered + distance;
                Toast.makeText(getActivity(),
                        "distance covered " + distanceCovered,
                        Toast.LENGTH_SHORT).show();

                oldLocation = location;

        }
    }

It will give the total distance covered whenever onLocationChanged is called.

Simple and straightforward.

Have a great day!

Upvotes: 0

Mike B
Mike B

Reputation: 5451

Just keep a running total. Instead of finding the distance from the new location to the firstlat,firstlon location, find the distance from the new location to the immediately previous location and add that distance to your total.

Create a member variable in the class that your updateDistance method is in that holds the previous location. Initially set it to the starting location. Also create a member variable to hold the total distance, initially set to 0. In your updateDistance method compute the distance between the given location and the previous location (which initially will be the starting location) and add that to the total distance. Then assign the current location to the previous location variable.

public class YourClass {
    private Location prevLocation = startingLocation;
    private double distance = 0d;

    private void updateDistance(Location location) {
        double distanceToLast = location.distanceTo(prevLocation);
        // if less than 10 metres, do not record
        if (distanceToLast < 10.00) {
            Log.i("DISTANCE", "Values too close, so not used.");           
        } else
            distance += distanceToLast;
            prevLocation = location;
        }
        tv1.setText("Distance Travelled: " + distance + " metres");
    }
}

Upvotes: 1

Related Questions