2016-07-25 16 views
1

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

这是我的代码。 但我想得到我的速度,行程距离以及最大和最小高度。 如果任何人都可以帮忙,请做,它将不胜感激!

+0

'protected void onCreate(Bundle savedInstanceState);'是那个';'一个错字? –

+0

其实不是:D –

回答

1

你可以在这里找到如何计算两个位置之间的距离: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()相应地更新它们。

+0

谢谢我会试试这个! –