-
Notifications
You must be signed in to change notification settings - Fork 0
Google Maps Fragment Guide
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.
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.
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.
After downloading play services we need to import it to Eclipse which will be used as a library for our maps project.
-
In Eclipse goto File ⇒ Import ⇒ Android ⇒ Existing Android Code Into Workspace
-
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
-
Importantly while importing check Copy projects into workspace option as shown in the below image.
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:
Now open Google API Console, select Services on left side and turn on Google Maps Android API v2.
Now select API Access on left side and on the right side click on Create new Android key…
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:
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.
- Download the Maps Demo application and extract the files.
- Run "File...Import...Existing Android Code Into Workspace" and hit "Finish"
- 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.
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:
- Sign up for an account on the Genymotion Website
- (Mac or Linux) Install VirtualBox, a powerful free virtualization software.
- Download Genymotion Emulator for your platform.
- Install the Genymotion Emulator
- Windows: Run the msi installer
- Mac: Open dmg and drag apps to Applications directory
- Run Genymotion application
- Sign in and add your first virtual device (Nexus S - 4.2.2 with Google Apps - API 17)
- 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
- Click the genymobile icon
and click "Start" on your virtual device.
- 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.
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.