Using Location Services in Android

Android Location ServicesNeed help with directions? Or want to know where you are? Google Maps is one of the great service that Google provides to the masses free of cost. Using Maps in your android application is easy as long as you know what you are doing. In this tutorial I will demonstrate a simple application which will display your current position in a map and update it every time you move.

Step 1: Get a Google Maps API key
Before doing anything you need to get a MD5 key which will allow you to use the API in your application. To get a key you first need to create a simple certificate fingerprint. For this tutorial I have created a SDK Debug Certificate which has to be used for development. I am using eclipse for developing this application so to create the certificate I first needed to locate my debug.keystore file. I found the location in Eclipse by using Window->Preferences->Android->Build. Once I got the location I ran a simple command on the command prompt:<!

$ keytool -list -v -alias androiddebugkey -keystore ~/.android/debug.keystore -storepass android -keypass android

Note:If you not using JDK7 then you need not use -v option

Now that I had the key I went to the registration page here and gave them my MD5 certificate fingerprint. It generated the key for me.

Step 2: Creating the Android Project

Location Services using Android
Now create the android project using the following settings:

To let your application use the GPS of your Android phone you need to add some permissions to the AndroidManifest.xml. The permissions are for accessing location and internet services.
So I did that and my manifest looks like this:

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=””
android:versionName=”1.0″ >

android:targetSdkVersion=”10″ />

<uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION” />
<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION” />
<uses-permission android:name=”android.permission.INTERNET” />
android:theme=”@style/AppTheme” >
android:label=”@string/app_name” >
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />


Step 3: Creating a View
To display a map in your Android layout, you need to add a MapView. So inside a LinearLayout we are adding the MapView:

<LinearLayout xmlns:android=””
android:orientation=”vertical” >

android:clickable=”true” />


Note: Replace MapAPIKey with your api key.
Also you need to specify that you are going to use the google maps library in the manifest. So I added the following line before the <activity> tag:

<uses-library android:name=”” />

Step 4: Writing the map activity
In order to use a MapView we must first extend MapActivity. So I created a simple Activity class extending the MapActivity like this:

package com.example.mymaps;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends MapActivity {
private MapView map;
private MapController controller;

protected void onCreate(Bundle savedInstanceState) {
//functions to be implemented

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(, menu)
return true;

protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
Next thing that I did was to initialize my MapView:
private void initMapView() {
map = (MapView) findViewById(;
controller = map.getController();

And lastly added my location overlay:

private void initMyLocation() {
final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
//overlay.enableCompass(); // does not work in emulator
overlay.runOnFirstFix(new Runnable() {
public void run() {
// Zoom in to current location


And then I smiled at myself and thought at least I will now know where I am.

LBS Android Map View
Some Guidelines:
•    Make sure that the AVD you use is Google API enabled
•    You can set the GPS coordinates using the DDMS in eclipse. The app will NOT show anything until you set the coordinates
•    Once you are sure your app is working fine don’t forget to sign it using a new certificate. The debug certificate is only for debugging!!

* The complete project is attached with the post.

Attachment Size 979.22 KB