how to calculate distance while walking in android?

前端 未结 7 1404
说谎
说谎 2020-12-04 16:16

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

相关标签:
7条回答
  • 2020-12-04 16:21

    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

    0 讨论(0)
  • 2020-12-04 16:23

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

    0 讨论(0)
  • 2020-12-04 16:29

    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.

    0 讨论(0)
  • 2020-12-04 16:33
    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;
    }
    
    0 讨论(0)
  • 2020-12-04 16:35

    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.

    0 讨论(0)
  • 2020-12-04 16:36

    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

    0 讨论(0)
提交回复
热议问题