Breaking News
Home / Android / Android Tutorials / Using Location Services in Android

Using Location Services in Android

Need help with directions? Or want to know where you are? Google Maps is one of the great services 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 on a map and update it every time you move.

Step 1: Get a Google Maps API key
Before doing anything you need to get an 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 an 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:

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 on 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 as shown below:

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:

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:::L

Lastly added my location overlay:

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 to the post.

Attachment Size 979.22 KB

Check Also

Building a TODO List app in Android using SQLite

Storing data locally in Android is very easy. Here are few ways we can do …

Advertisment ad adsense adlogger