Skip to content

Google Maps Fragment Guide

Nathan Esquenazi edited this page Nov 6, 2013 · 14 revisions

Overview

Many Android applications require the use of an embedded interactive map. On Android, this embedded map is part of the Google Play Services SDK which is a Google add-on pack for Android enabling all sorts of extra features around gaming, messaging, billing, location, etc.

In this guide, we will walk you through the step by step process of getting an embedded Google Map working within an Android emulator.

Download Google Play Services

First, let's download and setup the Google Play Services SDK. Open Eclipse ⇒ Windows ⇒ Android SDK Manager and check whether you have already downloaded Google Play Services or not under Extras section. If not select play services and install the package.

Play Services

Important Note

After the latest API update (version 19), if you run into any issues, please make sure to download 'Google Play Services for Froyo' instead of the 'Google Play services' since the latest library drops support for previous versions.

Import Google Play Services

After downloading play services we need to import it to Eclipse which will be used as a library for our maps project.

  1. In Eclipse goto File ⇒ Import ⇒ Android ⇒ Existing Android Code Into Workspace

  2. Click on Browse and select Google Play Services project from your android sdk folder. You can locate play services library project from \extras\google\google_play_services\libproject\google-play-services_lib

  3. Importantly while importing check Copy projects into workspace option as shown in the below image.

Google Play Load

Retrieve your SHA-1 Fingerprint

Open your terminal and execute the following command to generate SHA-1 fingerprint necessary to get your API key from Google.

On Windows:

keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android

On Mac or Linux:

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

In the output you can see SHA 1 finger print:

Fingerprint

Register for an API Key

Now open Google API Console, select Services on left side and turn on Google Maps Android API v2.

Services

Now select API Access on left side and on the right side click on Create new Android key…

Access

It will popup a window asking the SHA1 and package name. Enter your SHA 1 and your android project package name separated by semicolon ; and click on create. For now enter:

BE:03:E1:44:39:7B:E8:17:02:9F:7F:B7:98:82:EA:DF:84:D0:FB:6A;com.example.mapdemo

Note the API Key for use in the next step:

Key

Import Maps Demo

Now that we have our Genymotion emulator properly setup, let's import the demo application we can use to verify if maps are showing up correctly.

  1. Download the Maps Demo application and extract the files.
  2. Run "File...Import...Existing Android Code Into Workspace" and hit "Finish"
  3. Expand MapDemo application and open up the "AndroidManifest.xml"

Enter your API Key into the meta data for com.google.android.maps.v2.API_KEY:

<meta-data
   android:name="com.google.android.maps.v2.API_KEY"
   android:value="YOUR-KEY-HERE" />

Now we need to use Google Play Services project as a library to use project. So right click on project and select properties. In the properties window on left side select Android. On the right you can see a Add button under library section. Click it and select google play services project which we imported previously.

Library

Genymotion

The first step to getting Google Maps working on your emulator is to download a third-party emulator called Genymotion. The reason for this is that the official emulator does a terrible job of supporting the Google Play Services. While it is possible to get the Intel HAXM fast emulator working with the Play Services SDK, at the moment it's far more trouble then it's worth.

Genymotion is an incredibly fast, memory-efficient VM that runs the Android OS in a more accurate manner than even the official emulator. Many Android developers do all their device testing using this emulator especially when Google Play services is concerned.

To setup your genymotion emulator sign up and follow the installation guide:

  1. Sign up for an account on the Genymotion Website
  2. (Mac or Linux) Install VirtualBox, a powerful free virtualization software.
  3. Download Genymotion Emulator for your platform.
  4. Install the Genymotion Emulator
  • Windows: Run the msi installer
  • Mac: Open dmg and drag apps to Applications directory
  1. Run Genymotion application
  2. Sign in and add your first virtual device (Nexus S - 4.2.2 with Google Apps - API 17)
  3. Install the Eclipse Plugin
    • Go to the "Help/Install New Software..." menu
    • Add a new software site: Genymobile - http://plugins.genymotion.com/eclipse
    • Check all genymobile entries
    • Accept licenses and install
    • Restart eclipse
  4. Click the genymobile icon Genymobile and click "Start" on your virtual device.
  5. Wait for device to boot up and then run through initial setup

Note: On Ubuntu 12.04, make sure to 3D acceleration mode by launching VirtualBox and going to Settings -> Display to fix.

Conclusion

At this point, you should have the Google map displaying in your sample application. If you don't, try restarting the emulator and uninstalling / reinstalling the map demo application. Eventually you will see the maps if you registered your key properly.

For more information including how to use the maps, check out the source of this article on androidhive.

References

Clone this wiki locally