Google Maps for Android – Tutorial

Using Google Maps in Android Application includes several steps.

1. Install the Android SDK

As a prerequisite, you need to install the Android SDK. See Get the Android SDK.

2. Download and configure the Google Play services SDK, which includes the Google Maps Android API. If you use theGoogle Maps Mobile SDK for Business you must download and configure the Google Maps Mobile SDK for Business static library.

The Google Maps Android API v2 is distributed as part of the Google Play services SDK. You can download the Google Play services SDK via the Android SDK Manager.

To test your app when using the Google Play services SDK, you must use either:

  • A compatible Android device that runs Android 2.3 or higher and includes Google Play Store.
  • The Android emulator with an AVD that runs the Google APIs platform based on Android 4.2.2 or higher.
  • Add Google Play Services to Your Project
  • Create a Android Application Project.

Google Maps for Android

  • Import the Google Play Services into your workspace.

To import the Google Play Services into your workspace , first create a android project from existing resource.Then goto path ..sdk/extra/google/googleplayservice.

Google Maps for Android

Then copy the project in to the workspace..

  • Make this project as the library project of your project.
    Right Click on the project, then go to properties and the Android Section. Add google play service as library project.

Google Maps for Android

After you’ve added the Google Play services library as a dependency for your app project, open your app’s manifest file and add the following tag as a child of the element:

<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

3. Obtain an API key

Maps API keys are linked to specific certificate/package pairs, rather than to users or applications. You only need one key for each certificate, no matter how many users you have for an application. Applications that use the same certificate can use the same API key. However, the recommended practice is to sign each of your applications with a different certificate and get a different key for each one.

Obtaining a key for your application requires several steps.

  • Create key for your application

Google Maps for Android

Enter your SHA-1 fingerprint and the package of your application separated by a semicolon. For example you can use the org.aynsoft.mapDummy package.

Google Maps for Android

4. Add the API key to your application

Follow the steps below to include the API key in your application’s manifest, contained in the file AndroidManifest.xml. From there, the Maps API reads the key value and passes it to the Google Maps server, which then confirms that you have access to Google Maps data.

  • In AndroidManifest.xml, add the following element as a child of the element, by inserting it just before the closing tag :
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="API_KEY"/>

Substitute your API key for API_KEY in the value attribute. This element sets the keycom.google.android.maps.v2.API_KEY to the value of your API key, and makes the API key visible to anyMapFragment in your application.

  • Save AndroidManifest.xml and re-build your application.

Specify permissions

Specify the permissions your application needs, by adding elements as children of the element. The syntax is:

<uses-permission android:name="permission_name"/>

For example, to request the Internet permission, add:

<uses-permission android:name="android.permission.INTERNET"/>

Besides permissions required by other parts of your application, you must add the following permissions in order to use the Google Maps Android API:

Google Maps Android API uses OpenGL ES version 2 to render the map

<uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

5. Add a Map

  • 1. Add the following fragment in the app’s layout XML file. If you created a ‘hello world’ app using the Android Developer Tools (ADT) package in Eclipse, the file is at res/layout/activity-main.xml. Replace the entire contents of that file with the following code.
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"/>
  • 1. Add the following code in MainActivity.java.
package com.example.mapdemo;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Build and run your application. You should see a map. If you don’t see a map, confirm that you’ve completed all the steps described on this page.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Over the years we have developed several Google Map, mashups Android and iPhone components, code modules and Frameworks that can work as plug and play solutions for developing new apps that will substantially reduce the app development cost and time.

Contact us to discuss your multimedia mobile application development project.

Related Posts

Leave a comment

Hey, so you decided to leave a comment! That's great. Just fill in the required fields and hit submit. Note that your comment will need to be reviewed before its published.