web 2.0

Getting Current Location on Android SDK, requestLocationUpdates

Sorry that I've been MIA for past few month, didn't do any development and I was more focused on life. I am back to android development now and obviously I ran into few problems.

I've spent entire day trying to figure out how to get current location and yet all these forum posts were only giving half of the answer, so i decided to blog about it.

Step 1:
The permission is required on AndroidManifest.xml
please make sure

is there.

Step 2:
Listener is required due to the fact the last known location seems to be null all the time on the emulator. And somehow you need an empty class to accommodate that, I don't know why but it's just the way it is, and you can't declare new LocationListener() for some reason.

public class CurrentLocationListener implements LocationListener{

public void onLocationChanged(Location argLocation) {

}
public void onProviderDisabled(String provider) {
}

public void onProviderEnabled(String provider) {
}

public void onStatusChanged(String provider, int status, Bundle arg2) {
}
}


Step 3:
Declare Location Manager and Listener in order to get current latitude and longitude
in whichever class you decide to write them in.


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

LocationListener myLocationListener = new CurrentLocationListener();

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, myLocationListener);

Location currentLocation = locationManager.getLastKnownLocation("gps");

String Latitude = String.valueOf(currentLocation.getLatitude());
String Longitude = String.valueOf(currentLocation.getLongitude());


This worked for me up to this date, if it has any problems feel free to make comment on it.

I'll post about how to get data from wcf next time.

Tags: