how to calculate distance while walking in android?-ThrowExceptions

Exception or error:

I am developing a demo for my app, in which there are two buttons named as “START” and “STOP”. When user taps on “START” he will start walking. What I want to do is make it so that when users tap “STOP” then the demo will calculate his distance between “START” and “STOP”. If the user pressed “START” and pressed “STOP” without taking a single step, then it must show 0km or 0m. I don’t have any idea how I should start this; please make a suggestion.

How to solve:

One way to go about it is using the accelerometer data. Your app should continuously record the accelerometer data after the user presses the Start button. You will observe a peak in your data whenever the user takes a step. Apply a filter on this data, and you shall be able to detect the number of steps taken with reasonable accuracy. Multiply it by the step length and you should get an approximation of the distance travelled. Take height of the user as an input argument. Step length is around 0.45*Height of a person. Since this approach is independent of GPS, It will also work indoors.
You’ll need to use the accelerometer values for all three axes to make it fairly independent of the device orientation.You can go with x^2 + y^2 + z^2


There are different ways to do this:

  1. GPS: Keep adding GPS distance between 2 points every X seconds (say 10 sec). Check Android Location.distanceTo or distanceBetween. Check My Tracks app, it is open source. GPS is not available indoors and would have error if user is changing direction very frequently (read every 1-2 second)
  2. Accelerometer: Look for code/library for step detection using accelerometer. Distance comes from double integration of acceleration, errors can add up very quickly here.
  3. Step detector: Built-in in Nexus 5. Google must have taken care of accelerometer errors to extent possible. This is hardware-based computation, consumes less battery but not available in most of handsets as of date.

You can also check Pedestrian dead reckoning


Ask for GPS permissions in your app. When start is tapped, record the GPS coordinates. Do likewise for stop. You now have two coordinates. You can then apply the distance formula to get the total distance traveled.


As for the case clarified in the comments, I think what you need to look into is Android’s motion sensors. You may have to make a lot of assumptions or ask your users to calibrate your app before actual use.

Assume that you know your user’s pace factor. Using the motion sensor, time how long is the user “walking” (obviously, there’s no easy way to determine if your user is actually walking or just shaking the phone). Multiply this with your user’s pace factor and you get a pretty rough idea of how much walking has your user done.


Comment to “There is one problem with this solution. If you are traveling at constant speed, the acceleration is 0, so accelerometer wont pick-up any readings. – jnovacho 16 secs ago”
(sorry, don’t have enough reputation to comment directly)

when you accerlerate, save the accerleration and then calculate the speed you are walking. Stop calculation of speed whenever the acceleration changes and start over. If you stop, you should receive a negative accerleration, you’d then have to calculate if you just slowed down or stopped completely. But thats simply math 🙂


I had gone with the Gps method.
With the following steps:
On the click of start button, the latitude and longitude of the starting point were fetched and stored it in my dto with a proper TripId.

on the click of stop button :

TripDto dto = service.GetStartLatLong(TripIdA);
double lat = Double.valueOf(dto.getStartLati());
double lon = Double.valueOf(dto.getStartLongi());
Location locationa = new Location("point A");
double distance = location.distanceTo(locationa);

The distance returned by the location.distanceTo() method is in meters.


public double getDistance(double lat1, double lon1, double lat2, double lon2) 
    double latA = Math.toRadians(lat1); 
    double lonA = Math.toRadians(lon1);
    double latB = Math.toRadians(lat2); 
    double lonB = Math.toRadians(lon2);
    double cosAng = (Math.cos(latA) * Math.cos(latB) * Math.cos(lonB-lonA)) +
        (Math.sin(latA) * Math.sin(latB));
    double ang = Math.acos(cosAng);
    double dist = ang *6371;
    return dist;


Try using sensors for this, I feel you should not use GPS as it may not be so accurate.
Refer to the following open source pedometer project for what you are talking about.

Pedometer library

Will update this answer with more specified code if you want to go with sensor.

Leave a Reply

Your email address will not be published. Required fields are marked *