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