如何在android中行走时计算距离?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22292617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:47:05  来源:igfitidea点击:

how to calculate distance while walking in android?

androiddistancecalculator

提问by rahul

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.

我正在为我的应用程序开发一个演示,其中有两个名为“开始”和“停止”的按钮。当用户点击“开始”时,他将开始行走。我想要做的是当用户点击“停止”时,演示将计算他在“开始”和“停止”之间的距离。如果用户在没有走一步的情况下按下“START”并按下“STOP”,那么它必须显示 0km 或 0m。我不知道应该如何开始;请提出建议。

采纳答案by Navjot Singh

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.45*人的身高。由于这种方法独立于 GPS,因此它也适用于室内。编辑:您需要使用所有三个轴的加速度计值,使其与设备方向完全无关。您可以使用 x^2 + y^2 + z^2

回答by Sangharsh

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.distanceToor distanceBetween. Check My Tracksapp, 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.
  1. GPS:每 X 秒(比如 10 秒)继续在 2 个点之间添加 GPS 距离。检查 Android Location.distanceTo或 distanceBetween。检查My Tracks应用程序,它是开源的。GPS 在室内不可用,如果用户非常频繁地改变方向(每 1-2 秒读取一次),则会出现错误
  2. 加速度计:使用加速度计寻找用于步进检测的代码/库。距离来自加速度的双重积分,这里的误差加起来非常快。
  3. 步长检测器Nexus 5内置。谷歌必须尽可能地处理加速度计错误。这是基于硬件的计算,消耗更少的电池,但迄今为止在大多数手机中都不可用。

You can also check Pedestrian dead reckoning

您还可以检查行人航位推算

回答by skytreader

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.

在您的应用中请求 GPS 权限。点击开始后,记录 GPS 坐标。停止也是如此。您现在有两个坐标。然后,您可以应用距离公式来获得行驶的总距离。

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.

至于评论中澄清的案例,我认为您需要研究的是Android的运动传感器。在实际使用之前,您可能需要做出很多假设或要求您的用户校准您的应用程序。

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.

假设您知道用户的步速因素。使用运动传感器,计算用户“行走”了多长时间(显然,没有简单的方法可以确定您的用户是真的在行走还是只是在晃动手机)。将其与用户的步速系数相乘,您就可以大致了解用户完成了多少步行。

回答by swiknaba

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)

评论“此解决方案存在一个问题。如果您以恒定速度行驶,则加速度为 0,因此加速度计不会读取任何读数。– jnovacho 16 秒前”(抱歉,没有足够的声誉发表评论直接地)

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

当你加速时,保存加速,然后计算你行走的速度。每当加速度发生变化时停止计算速度并重新开始。如果你停下来,你应该得到一个负加速度,然后你必须计算你是只是放慢了速度还是完全停止了。但这只是数学:)

回答by Nishit Joshi

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.

我已经使用了 Gps 方法。使用以下步骤: 单击开始按钮,获取起点的纬度和经度并将其存储在我的 dto 中,并使用适当的 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.

location.distanceTo() 方法返回的距离以米为单位。

回答by Dinesh Bagvan

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;
}

回答by Harshal Benake

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.

尝试为此使用传感器,我觉得您不应该使用 GPS,因为它可能不那么准确。关于你所说的,请参考以下开源计步器项目。

Pedometer library

计步器库

Will update this answer with more specified code if you want to go with sensor.

如果您想使用传感器,将使用更多指定的代码更新此答案。