Wednesday, May 25, 2011

Basic calls for getting the location data from Android

The following is some basic calls to the GPS to retreive either the WIFI or cellular locations. The following code is NOT battery optimized, but serve as the starting point for location retreival

LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateLocation(location);  // define your own function here
}

public void onStatusChanged(String provider, int status, Bundle extras) {
Log.v(TAG, "onStatusChanged");
}

public void onProviderEnabled(String provider) {
Log.v(TAG, "onProviderEnabled");
}

public void onProviderDisabled(String provider) {
Log.v(TAG, "onProviderDisabled");
}
};

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
Constant.GPS_REQUEST_TIME, 0, locationListener);
} else {
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
Constant.GPS_REQUEST_TIME, 0, locationListener);
}


The location object will give you access to the longtitude and latitude.

No comments:

Post a Comment