• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Get traveled distance using GPS

SG1

Lurker
Hello.

I would like to record my traveled distance in meters using gps, when my speed would be grater then 0.

I got my speed with speed = location.getSpeed() * 3.6; //in km/h

Would like to do:

if(speed > 0)
//start recording my distance in meters

I only found how to calculate distance between two points.
 
You could simply record the starting location (say you call it startLoc) and the end location (say you call it endLoc) and then call startLoc.distanceTo(endLoc)...
 
lala... Read it all before answering Jam....

Well...

You could still use that approach, just do that calculation everytime your LocationManager fires an locationUpdated event, and add the new positions distance to the last know position to a total...
 
lala... Read it all before answering Jam....

Well...

You could still use that approach, just do that calculation everytime your LocationManager fires an locationUpdated event, and add the new positions distance to the last know position to a total...

Thank you for your idea.
This is what i did (its not working....get force close):

protected void onResume() {
// TODO Auto-generated method stub
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500,1, this);
trenutnip = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
super.onResume();
}
public void onLocationChanged(Location location) {
razdalja = (int)trenutnip.distanceTo(location);
HomeTest.setText(trenutnip.toString());}

What i'm doing wrong?
 
I found out that trenutnip=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); is NULL.

I dont understand why?
 
My experience, from several projects is that it is always best to initiate a location manager let it's events keep the current location updated, to make sure you always have a somewhat new location, that your app controls. I mostly do it something like this:

Code:
private LocationManager locMan;
private LocationListener locLis;
private Location currentLocation;

/**
 * Starts the location service and registers listeners
 * By JamTheMan - android.jamtheman.dk
 */
private void initLoactionManagement(){
	locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
	locLis = new LocationListener() {
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
		public void onProviderEnabled(String provider) {
		}
		public void onProviderDisabled(String provider) {
		}
		public void onLocationChanged(Location location) {
			updateLocation(location);
		}
	};
	locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locLis); //<-- From network?
	locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locLis); //<-- GPS Only?
}

/**
 * Updates the location 
 * @param location	The currently read location
 * By JamTheMan - android.jamtheman.dk
 */
private void updateLocation(Location location){
	System.out.println("Updating Location -- " + location.getLatitude() + ":" + location.getLongitude());
	currentLocation = location;
}

This makes sure that the variable currentLocation gets updated every time there is a new location available.
 
Back
Top Bottom