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.
9 Answers
There are different ways to do this:
- 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)
- Accelerometer: Look for code/library for step detection using accelerometer. Distance comes from double integration of acceleration, errors can add up very quickly here.
- 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
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. EDIT: 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

- 497
- 2
- 9
-
2There 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 Mar 10 '14 at 08:41
-
7@jnovacho Its not about your travelling speed in one particular direction. When you take a step, There is a sudden change in acceleration with the phone in your pocket(Mostly in Z-Axis, but also in X and Y axis). Its not because of your walking speed, Its because of you taking a step forward which is a sudden motion. – Navjot Singh Mar 10 '14 at 08:48
-
1@jnovacho - the case you are mentioning about would occur when the user is holding the device still and moving. When the user is keeping the device in the pocket, because of user's leg movement there is a pattern found in the acceleration values. – Utkarsh Mankad Mar 25 '15 at 10:33
-
@UtkarshMankad There's also a pattern -- smaller, but still visible -- when holding it, because the whole body moves up and down with each step. – Nic Oct 19 '16 at 14:39
-
@ Fund Monica's Lawsuit, you're right body moves up an down. To avoid step count when a body is moving up and down, you can validate the pattern. For example, float accelationSquareRoot = (x * x + y * y + z * z) / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH); then use its value in if statement like if(accelationSquareRoot >2.0){ //do count your step here } . . . . – Yosidroid May 12 '20 at 15:39
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.
Edit:
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.

- 11,467
- 7
- 43
- 61
-
-
4Thanks skytreader, I thought of doing the same but the problem is that if I start from point A and ends at point A after walking 3 kms then my start and ending coordinates would be same and it will return me 0km on applying distance formula – rahul Mar 10 '14 at 05:27
-
I haven't done a lot of GPS programming but AFAIK, that depends on _a lot_ of factors. The accuracy primarily depends on the phone's hardware itself and the GPS service/satellite you are using. – skytreader Mar 10 '14 at 05:28
-
@rahul for the situation you are describing, you can handle it logically. You can get location every time whenever user moves, and try to recalculate distance for every change in position instead of calculating distance at start and end. When user press stop you should stop calculating distance. – Gaurav Gupta Mar 10 '14 at 05:31
-
1
-
Gaurav's comment would be, admittedly, more accurate than my rough solution in the edit but depending on your settings, this can drain battery quickly. – skytreader Mar 10 '14 at 05:34
-
Your idea is nice, but you need improvement in this. Because when user travel in a circle then your distance will be result in 0 as starting point and ending points are same. – user2060383 Mar 10 '14 at 05:35
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 :)

- 83
- 1
- 7
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");
locationa.setLatitude(lat);
locationa.setLongitude(lon);
double distance = location.distanceTo(locationa);
The distance returned by the location.distanceTo() method is in meters.

- 111
- 3
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.
Will update this answer with more specified code if you want to go with sensor.

- 9,715
- 11
- 67
- 131

- 2,391
- 1
- 23
- 40
-
4It's the opposite, using sensors to measure distance is extremely inaccurate. You would be better off with GPS... for sure! – Merlevede Mar 10 '14 at 05:33
-
actully i saw following app it worked for me. https://play.google.com/store/apps/details?id=de.j4velin.pedometer – Harshal Benake Mar 10 '14 at 05:34
-
@Merlevede I went though gps and network providers few times.But it happens that sum times it gives very inaccurate results.So I suggested sensors after going through few pedometer apps.I would like to have your opinion on this. – Harshal Benake Mar 10 '14 at 05:37
-
I've read Navjot's answer below, and I think these apps use sensors to detect **a step** based on an impulse or peak. This may be feasible. On the other hand I was talking about several attempts that have been made to measure distance with pure accelerator data, and it has proven to be very inaccurate. Check [here](http://stackoverflow.com/questions/17572769/calculating-distances-using-accelerometer) and [here](http://stackoverflow.com/questions/6352681/how-to-use-accelerometer-to-measure-distance-for-android-application-development) – Merlevede Mar 10 '14 at 05:41
-
@HarshalBenake: The step sensor added was added in the latest API 19 (KitKat 4.4). And I am not sure if a backport was done. In which case an app using this feature would work only on devices with the latest API (notably, the Nexus 5). Big downside in my view. – Siddharth Lele Mar 10 '14 at 05:43
-
@Merlevede So what you suggest.What will be the perfect solution to calculate distance. – Harshal Benake Mar 10 '14 at 05:43
-
I think for large distances GPS works nice. For short distance (a few meters) the *step counting* approach would work nicer. – Merlevede Mar 10 '14 at 05:44
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;
}

- 10,802
- 4
- 33
- 38

- 129
- 1
- 3
-
1Try adding comments about how to implement instead of only giving a code – E.Akio Nov 20 '20 at 12:16
You can find the Latitude and Longitude of the current location using START button using location manager and store it in the variables. Then find the latitude and longitude of your end point using same method. Find the Distance between them by using this -

- 9
- 1
- 1
if your track is not a direct way (curve or zigzag) then you should use check location every 3-10 second some one else say before me (x second).

- 1
- 6