java - 使用 LocationListener 的带 GPS 的运动跟踪器

标签 java android gps locationlistener

我正在尝试制作一个跟踪用户移动的应用程序。 到目前为止,我有一个显示位置和“速度”的应用

protected void onCreate(Bundle savedInstanceState);
setContentView(R.layout.main);

txt = (TextView)findViewById(R.id.textView);
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

LocationListener locationListener = new MyLocationListener();
locationListener.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000,10,locationListener);

}
private class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location loc){
String longitude = "Long: "+ loc.getLongitude();
String latitude = "Lat: "+ loc.getLatitude();
txt.setText(longitude + latitude);
}

这是我的代码。 但我想知道我的速度、行程距离以及最大和最小高度。 如果有人可以提供帮助,请提供帮助,我们将不胜感激!

最佳答案

您可以在这里找到如何计算两个位置之间的距离:Calculating distance between two geographic locations .我会计算 onLocationChanged 中每个位置之间的距离,并将这些距离相加以获得 tripDistance。

当你有距离时,很容易通过距离除以时间来计算速度:

long startTime = System.currentTimeMillis(); //(in onCreate()
long currentTime = System.currentTimeMillis(); //(in onLocationChanged())
long deltaTimeInSeconds = (currentTime - startTime) * 1000;
double speed = tripDistance / deltaTimeInSeconds;

要获得高度,您可以使用 loc.getAltitude();。您可以有两个变量:double minAltitude, maxAltitude; 并在每个 onLocationChanged() 中相应地更新它们。

关于java - 使用 LocationListener 的带 GPS 的运动跟踪器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38566508/

相关文章:

iphone - 为什么即使我在后台请求位置更新,我的 iOS 应用程序也会被杀死?

java - 更改 tomcat session ID 以包含大写和小写字符

android - 在锁定屏幕顶部显示 View 的标志

java - Swing:随时间变化绘制可用性图表

android - 允许我的应用程序用户从单个帐户发布推文

java - onMapReady 并不总是被调用

java - LocationManager requestLocationUpdates minTime OR minDistance

android - 将位置放在用户周围一定半径的 map 上 - android

java - maven构建时如何阻止从其他存储库下载metadata.xml

java - 一次从 .txt 获取 16 个字节