Getting the location of your Android device
In this post you’ll learn about how to read out the Location on your Android device.
Requirements
- Android device
Manifest Permission
First we start with adding the right permission to our Manifest.xml file, otherwise the application simply won’t work.
The following lines need to be added to the file:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" ></uses-permission>
<uses-permission android:name="android.permission.ACCES_FINE_LOCATION" ></uses-permission>
The difference between the two are, the first permission is not that accurate as the second permission. This is because the first one uses the telephone signal and probably wifi networks, and the second uses the GPS sensor in your Android device.
The reason for this separation is that GPS uses a lot more battery power than the other two. So by getting your location with the GPS every time should drain your battery very fast.
After this we are done with the Manifest.xml file.
The java part
Your main class needs to implement the locationlistener interface, so you’ll need to add the following to your java class.
public class MainActivity implements LocationListener {
}
After this we need to implement and override the methods from this interface, the methods are:
- onLocationChanged(Location location)
- What to do when the location has changed, here is where most of the magic happens.
- onStatusChanged(String provider, int status, Bundle extras)
- What to do when the status of the Location device has changed
- onProviderEnabled(String provider)
- What to do when the provider is enabled.
- onProviderDisabled(String provider)
- What do to when the provider is disabled, you probably want to redirect the user to their location settings.
In your code it probably looks like this:
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
After we implemented the interface is time to do some real coding.
The code
First we need to declare and initialize the LocationManager, Location and Provider and two strings to store our data.
private String provider;
private LocationManager locationManager;
private Location location;
private String longitude;
private String latitude;
And in the onCreate() function we initialize the variables.
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
To get the best location provider, we needed to add a criteria with a set of rules to determine what the best criteria is for our application. For more information see the Android SDK.
After this it’s probably a good time to implement some security checks, to see of the provider exist and to check if we can get a location.
if(provider != null)
{
locationManager.requestLocationUpdates(provider, 400, 1, this);
location = locationManager.getLastKnownLocation(provider);
}
else
{
onProviderDisabled(provider);
}
if(location != null)
{
onLocationChanged(location);
}
In this line of code we check if the provider is enabled or exist, we add the location to our location variable and we call the onLocationChanged() function and set to location as a parameter of that function. This function gets called every time the location of the device has changed.
At this point we’re gonna store the location into the two string we made earlier. We can do this by editing our onLocationHasChanged() function to the following:
@Override
public void onLocationHasChanged(Provider provider)
{
longitude = String.valueOf(location.getLongitude());
latitude = String.valueOf(location.getLatitude());
Toast.makeText(this, "Longitude: " + longitude + " " + "Latitude: " + latitude, Toast.LENGTH_LONG).show();
}
We get the location from our location variable and store them into our longitude and latitude. Before we could to do that, we changed the variable type from double to string. And after that we showed our location in a toast message.
For the finishing touch we need to add two more thing to our code. At this point our application gets our location even when the app is closed. It only stops when the app is force closed by the user. We don’t want this and we need to take care of that. Luckily the Android framework has two functions, namely the onPause() and onResume() function.
We can do this by adding the following lines to our code:
@Override
protected void onPause()
{
super.onPause();
locationManager.removeUpdates(this);
}
@Override
protected void onResume()
{
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
After this we are done with our code, and you experiment further on your own.
Sources
- http://developer.android.com/reference/android/location/Criteria.html
- http://developer.android.com/reference/android/location/Location.html
- http://www.vogella.com/tutorials/AndroidLocationAPI/article.html