Show Map in Android

Earlier we studied about how to get current geographic location using an android application. That Android app will return latitude and longitude pair to represent current location. Instead of simply showing these location as coordinates in an android application, it would be cool if we tile these coordinates on top of a map fragment.

In this Android tutorial, lets see about how to show a map in Android. Following are the steps that needs to be done.

Download and configure Google Map API.

Get certificate fingerprint of android application.

Create API project for the Android application.

Get Google Maps API key

Android manifest settings

Download and Configure Google Map API

We can download and install Google Map API using Android SDK manager. This API is provided with Google Play Services packages. So we need to run SDK manager to select this package that can be found under SDK package Extras as shown in the following figure.

Install Google Play Services

After installing, we should restart eclipse and should import this Google play services project into our Eclipse work space as a library to add reference to the dependency JAR files with our Android map application.

Get Certificate Fingerprint of Android Application.

Certificate finger print is needed to get new Android API key for our application. Application’s certificates are created in two mode.(i.e.) debug mode and release mode. To create a debug mode certificate, we need not sign it using a private key. It can be signed by the build tools with a special key created by the Android SDK build tools. The release certificate will be created when we export our application for release and should be signed by a private key.

Getting Debug Certificate Fingerprint

To get certificate finger print in debug mode, we can use the existing keystore debug.keystore file. The location is varies based on the operating system. For example, in Windows, this file will be located where .android is located.

After locating the keystore, then it’s time to enter the following line into the command prompt by replacing the keystore path. We are using the keytool utility provided with the JDK.

Getting Release Certificate Fingerprint

Let us enter the following line in command prompt by replacing keystore-name and alias-name as per our application.

keytool -list -v -keystore keystore_name -alias alias_name

Create API project for our map Android application.

After getting the application’s certificate finger print by signing our application using, any one of the available modes as we discussed in previous step, this finger print will be used to create new Android API key.

For that a console page provided by Google is used. Let us visit the Google API Console page to create new API project for our android application.

If we are creating our first API project, then we should click Create Project from left panel and should accept terms and conditions for this first time only. Otherwise, all created projects are listed with the drop down box which also contains Create link to add subsequent new API project.

After that, the next step is to enable the required services that will be listed on selecting services option from left panel. Now, it’s time to create android API key.

Get Google Maps API key

Google maps API key will be created by selecting API Access->Create New Android key. It will open a wizard which contains an input control to enter the certificate fingerprint that we got from step 2 and the name of our android application’s package separated with semicolon. For example,

And then, the console will respond with set of keys generated. From them, we can copy API key with 40 characters next to Key for Android apps (with certificates) caption.

Android Manifest Settings

And Finally, we can start coding with our application that is for showing map into our Android device. For that, some set of permissions are provided with Android manifest file.

First, API key that is given with the Google console response is added within <application>…</application> tags of manifest file as shown below.

After setting Android APT key and permission to manifest, a reference should be added for OpenGL ES version since Google Maps API require this specification for supporting map related application. For example,

Example: Showing Map Fragment using Android

This example project will be run using Android devices lower than 10 also. For that, we are using SupportMapFragment class instead of MapFragment to show map into the layout. So the layout file contains the following lines.

And then, we are using GoogleMap for creating map instance. For this instance, a reference is added to denote the obtained map fragment using getSupportFragmentManager(). For that, we are importing GoogleMap and SupportMapFragment classes provided as part of com.google.android.gms.maps package. The following code is for this Android Activity to obtain map.

I have a project in android which i send data to a php api by post method and receive a message. But when i debug my project it give me error the android.jar file has no source attachment …Pls help asap

[…] This Android tutorial will help a beginner to start with Google Maps on an Android device. We will be using Google Maps Android API V2 and it is an introductory level tutorial. We will show some chosen locations in Google map using Latitude and Longitude and draw lines (polyline) between them. This is continuation of the earlier written tutorial to show a map using Google Maps Android API. […]