Google

Tuesday, July 13, 2010

Android Location Manager




Download a gpx file and put in any folder, in this case GPX Files folder. Then load it from DDMS Emulator GPX tab.

Below is the main activity file. No xml layout file needed. Run the bottom part of GPX file which is collection of points by clicking on the play button of the DDMS. This simulates GPS tracking.

 package com.lmda;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
public class LocationManagerDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocationManager locMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locListener = new LocationListener() {
public void onLocationChanged(Location location) {
if (location != null) {
Toast.makeText(
getBaseContext(),
"New location latitude [" + location.getLatitude()
+ "] longitude [" + location.getLongitude()
+ "]", Toast.LENGTH_SHORT).show();
}
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
};
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, // minTime
// in ms
0, // minDistance in meters
locListener);
}
}