1

I am trying to make an app that tracks user movement. so far i have an app that shows the location and the "speed"

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

this is my code. but i want to get my speed, trip distance and max and min altitude. if ANYONE can help, please do, it will be greatly appreciated!

1 Answers1

1

You can find here how to calculate distance between two locations: Calculating distance between two geographic locations . I would calculate distance between every location in onLocationChanged and add those distances to have tripDistance.

When You have distance it is easy to calculate speed by dividing distance by time:

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

To have altitude you can use loc.getAltitude();. You can have two variables: double minAltitude, maxAltitude; and in every onLocationChanged() update them accordingly.

Community
  • 1
  • 1
Natalia
  • 681
  • 5
  • 12