how to calculate distance while walking in android?
Asked Answered
C

9

24

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.

Couplet answered 10/3, 2014 at 5:23 Comment(0)
B
17

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

Boil answered 10/3, 2014 at 5:33 Comment(5)
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.Bohon
@Bohon 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.Boil
@Bohon - 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.Damper
@UtkarshMankad There's also a pattern -- smaller, but still visible -- when holding it, because the whole body moves up and down with each step.Ajmer
@ 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 } . . . .Hugues
A
23

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

Alkalify answered 10/3, 2014 at 8:46 Comment(0)
B
17

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

Boil answered 10/3, 2014 at 5:33 Comment(5)
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.Bohon
@Bohon 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.Boil
@Bohon - 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.Damper
@UtkarshMankad There's also a pattern -- smaller, but still visible -- when holding it, because the whole body moves up and down with each step.Ajmer
@ 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 } . . . .Hugues
B
7

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.

Buller answered 10/3, 2014 at 5:26 Comment(7)
Is GPS accurate enough to keep track of couple of foot distance.Adenine
Thanks 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 formulaCouplet
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.Buller
@Couplet 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.Adenine
@Gaurav Thanks, this seems to be a solution for my problemCouplet
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.Buller
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.Harty
I
3

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 :)

Ida answered 10/3, 2014 at 8:46 Comment(0)
B
3

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.

Billposter answered 7/4, 2014 at 14:36 Comment(1)
I am late though but this applies to a straight line motion.Gesundheit
M
1

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.

Marcheshvan answered 10/3, 2014 at 5:32 Comment(7)
It's the opposite, using sensors to measure distance is extremely inaccurate. You would be better off with GPS... for sure!Freeboard
actully i saw following app it worked for me. play.google.com/store/apps/details?id=de.j4velin.pedometerMarcheshvan
@Freeboard 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.Marcheshvan
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 and hereFreeboard
@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.Moneychanger
@Freeboard So what you suggest.What will be the perfect solution to calculate distance.Marcheshvan
I think for large distances GPS works nice. For short distance (a few meters) the step counting approach would work nicer.Freeboard
G
1
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;
}
Gagliano answered 10/3, 2014 at 6:14 Comment(1)
Try adding comments about how to implement instead of only giving a codeCatha
M
0

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 -

https://www.geeksforgeeks.org/program-distance-two-points-earth/#:~:text=For%20this%20divide%20the%20values,is%20the%20radius%20of%20Earth.

Milkfish answered 5/1, 2021 at 8:55 Comment(0)
S
0

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).

Seda answered 20/5, 2021 at 17:14 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.