Android Location API


Determine the current geolocation

Most Android devices allow to determine the current geo location. This can be done via a GPS (Global Positioning System) module, via cell tower triangulation and via wifi networks.

Google Play provides the fused location provider to retrieve the device’s last known location.

To use the location manager make the Google play service available via your app build.gradle file.

Also specify the following required permission in your manifest.

Using the LocationManager
Now you can access the last known location. The fuse location provider provides a new simple API. The following is an example activity which uses it.

1.4. Forward and reverse Geocoding
The Geocoder class allows to determine the geo-coordinates (longitude, laditude) for a given address and possible addresses for given geo-coordinates.

This process is known as forward and reverse geocoding. The Geocoder class uses an online Google service.

If you want to access the GPS sensor, you need the ACCESS_FINE_LOCATION permission. Otherwise you need the ACCESS_COARSE_LOCATION permission.

Prompt the user to Enabled GPS
The user can decide if the GPS is enabled or not.

You can find out, if a LocationManager is enabled via the isProviderEnabled() method. If its not enabled you can send the user to the settings via an Intent with the Settings.ACTION_LOCATION_SOURCE_SETTINGS action for the android.provider.Settings class.

Typically you would open an AlarmDialog prompt the user and if he wants to enable GPS or if the application should be canceled.

You cannot enable the GPS directly in your code, the user has to do this.

Using GPS and setting the current location
Activating GPS on the emulator
You need to activate GPS on your test device. If you test on the emulator and its not activated you “null” if you try to use a LocationManager.

The Google Map activity should automatically activate the GPS device in the emulator but if you want to use the location manager directly you need to do this yourself. Currently their seems to be an issue with this.

Start Google Maps on the emulator and request the current geo-position, this will allow you to activate the GPS. Send new GPS coordinates to the Android emulator.

Using the Android Location API
Create Project
Create a new project called with the Activity called ShowLocationActivity.

This example will not use the Google Map therefore, it also runs on an Android device.

Change your layout file from the <filename class=”directory”>res/layout_ folder to the following code.

Add permissions
Add the following permissions to your application in your AndroidManifest.xml file




Change ShowLocationActivity to the following. It queries the location manager and display the queried values in the activity.


Leave a Reply