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 ma
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
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 :)
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.
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;
}
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.
There are different ways to do this:
You can also check Pedestrian dead reckoning