Skip to content Skip to sidebar Skip to footer

How To Use Android DistanceBetween() Method

Hi recently I have been working with the application that requires to track the distance the person traveled while he is walking. I am using the location manager for getting the pe

Solution 1:

I am confused how to distinguish starting position and end position, as location manager gives you the current position and keeps giving back new position every 5s

You already have the code available to get a new location, so you need to save the old Location and calculate distance to new location during every location update.

Location oldLoc, newLoc;
locationListener = new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        if (oldLoc!=null) { 
            if (newLoc == null) { 
                newLoc = location; 
                // get distance 
                return; 
             } 
            // else, both not null 
            oldLoc = newLoc;
            newLoc = location;
            // get distance 
        } else { oldLoc = location; } 
    }

Post a Comment for "How To Use Android DistanceBetween() Method"