how to calculate distance while walking in android

2019-01-13 03:23发布

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.

7条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-01-13 04:02
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;
}
查看更多
Bombasti
3楼-- · 2019-01-13 04:08

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.

查看更多
我想做一个坏孩纸
4楼-- · 2019-01-13 04:16

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.

查看更多
淡お忘
5楼-- · 2019-01-13 04:20

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.

查看更多
三岁会撩人
6楼-- · 2019-01-13 04:21

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

查看更多
我欲成王,谁敢阻挡
7楼-- · 2019-01-13 04:26

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

查看更多
登录 后发表回答