Your feedback is appreciated..Send Feedback Thank You.

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=”http://schemas.android.com/apk/res/android”
package=”com.example.mymaps”
android:versionCode=”1″
android:versionName=”1.0″ >

<uses-sdk
android:minSdkVersion=”9″
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” />
<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”com.example.mymaps.MainActivity”
android:label=”@string/app_name” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>

</manifest>

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=”http://schemas.android.com/apk/res/android”
android:id=”@+id/frame”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”vertical” >

<com.google.android.maps.MapView
android:id=”@+id/map”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:apiKey=”MapAPIKey”
android:clickable=”true” />

</LinearLayout>

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=”com.google.android.maps” />

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 com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

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

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//functions to be implemented
initMapView();
initMyLocation();
}

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

@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
[/code]
Next thing that I did was to initialize my MapView:
[code]
private void initMapView() {
map = (MapView) findViewById(R.id.mapview);
controller = map.getController();
map.setSatellite(true);
map.setBuiltInZoomControls(true);
}

And lastly added my location overlay:

private void initMyLocation() {
final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
overlay.enableMyLocation();
//overlay.enableCompass(); // does not work in emulator
overlay.runOnFirstFix(new Runnable() {
public void run() {
// Zoom in to current location
controller.setZoom(8);
controller.animateTo(overlay.getMyLocation());
}
});
map.getOverlays().add(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 with the post.

Attachment Size
MyMaps.zip 979.22 KB
Subscribe to DeveloperFeedDid you like this article? Did it Help you Solve your Problem? You can get the all the latest articles published at DeveloperFeed in your email inbox by entering your email address below. Your address will only be used for mailing you the articles, and each one will include a link so you can unsubscribe at any time.