Developing with Google Maps v2 for Android

In one of my previous articles, I talked about embedding Google Maps in Android applications. Much has changed as Google went about revamping a lot of existing APIs in Android. In version 1 of Google Maps for Android, Google used the MapView to display map data. In version 2, the MapView is deprecated; instead, you have to use a MapFragment. In addition, you no longer need to create a project that targets the Google APIs platform, as Google has migrated a lot of services into Google Play services.

In this article, I will walk you through the steps needed to get Google Maps v2 for Android to work, and show you some of the new APIs you can use in your applications.

Install the Google Play Services Library

The first step in using Google Maps v2 for Android is to download the Google Play Services SDK. The Google Play Services SDK contains the APIs needed by Google Maps and hence you need to download this and reference it in your Android application.

  1. In Eclipse, select Windows > Android SDK Manager.
  2. Check the Google Play services item located under the Extras folder (see Figure 1):

    Android SDK Manager
    Figure 1 – Downloading the Google Play services SDK

  3. Click the Install 1 package… button
  4. This will install the Google Play services library onto the following folder:

Import the Google Play Services Library project into your workspace

The next step is to import the Google Play Service Library project into your workspace so that you can import it into your Android project.

  1. In Eclipse, select File > Import…
  2. Select Android > Existing Android Code, and browse to location of the Google Play services library.
  3. In the Import Projects dialog, check the Copy projects into workspace option and then click Finish (see Figure 2):

    Android project import dialog
    Figure 2 – Importing the Google Play Services Library into the workspace

  4. You should now see the google-play-services_lib project imported into your workspace (see Figure 3).

    Eclipse package explorer
    Figure 3 – The imported Google Play Services Library

Create the Android Project

You are now ready to create an Android Project to use the new Google Maps v2.

  1. In Eclipse, create an Android Application Project and name it as follows (see Figure 4):

    Android application creation dialog
    Figure 4 – Creating the Android Project

    As mentioned earlier, it is not necessary to choose the Google APIs for using Google Maps v2 for Android.

  2. Click on the Next button and then the Finish button for the next couple of dialogs.
  3. Right-click on the GoogleMaps project and select Properties.
  4. Select the Android item on the left and click the Add… button on the right (see Figure 5):


    Figure 5 – Adding a Library to the project

  5. Select the google-play-services_lib project and click OK and then OK (see Figure 6):

    Android library selection dialog
    Figure 6 – Selecting the Google Play Services Library

Enabling Google Maps for your Application

To use Google Maps v2 for Android, you need to enable it in the Google APIs Console window.

  1. Using the Web browser on your computer, go to the Google APIs Console located at https://code.google.com/apis/console.
  2. Click the Create… item to create a new API project (see Figure 7):

    Google APIs console project menu
    Figure 7 – Creating a new project in the Google APIs Console window

  3. Give a name to your project and click the Create project button (see Figure 8):

    Google APIs console new project dialog
    Figure 8 – Naming the project

  4. You will now be able to see a list of services available. Locate the Google Maps Android API v2 item and turn on the service (see Figure 9):

    Turning on Google Maps Android API v2
    Figure 9 – Turning on the Google Maps v2 for Android service

Getting the Maps API Key

In order to use Google Maps v2 for Android, you need to apply for a Maps API key. In order to apply for a Maps API key, you need to pass Google the SHA1 hash of the certificate that you will use for signing your app.

  1. For Mac users, launch a Terminal window and follow the instruction in the next step. For Windows users, launch a Command window and cd to the bin folder of the Java installation folder. The bin folder is typically in the following directory:


    C:Program FilesJavajdk1.7.0_21bin>

  2. Issue the following command to extract the SHA1 hash on your keystore (see Figure 10):


    keytool -list -alias androiddebugkey -keystore "<path of debug.keystore>" -storepass android -keypass android –v

    Extract SHA1 hash from certificate
    Figure 10 – Extracting the SHA1 hash from the certificate

  3. Back in the Google APIs Console page, click on the API Access item on the left and click the Create new Android key… button (see Figure 11):

    Generating Android key in Google APIs console
    Figure 11 – Generating a new Android key

  4. Enter the SHA1 key obtained previously followed by a “;” and then the package name of your project. Click Create (see Figure 12):

    Configure key for Android project
    Figure 12 – Entering the SHA1 hash and package name

  5. You will now obtain the Google Maps API key (see Figure 13):

    Obtain Google Maps API key
    Figure 13 – Obtaining the Google Maps API key

  6. Copy the Maps API key and save it somewhere. You will need this key later on.
  7. Back in Eclipse, enter the following code in AndroidManifest.xml. Be sure to take note of the package name of your project as well as put it the Maps API key that you have obtained in the previous step:

  8. In the activity_main.xml file, replace the content of the file with the following statements:

  9. Deploy the application onto a real Android device.

    At the moment, the Android emulator does not support Google Maps v2 for Android as it needs access to Google Play on the emulator.

  10. You should see the map as shown below (see Figure 14):

    Google Maps displayed in Android app
    Figure 14 – The Google Maps displaying in your Android application

Using the Android Support Package for Google Maps

To ensure that your application is able to run on older Android devices, it is necessary to change the MapFragment class in your activity_main.xml file to use the SupportMapFragment class from the Android Support Package.

  1. Update activity_main.xml to look like:

  2. Add the following statements to the MainActivity.java file:

Your Google Maps will now be able to run on older versions of Android.

Programmatically Manipulating the Map

In the following sections, I will show you some of the common operations that you can perform with Google Maps.

  1. Add the following statements in the main.xml file located in the res/menu folder:

  2. Update MainActivity.java with the following code:

The above code creates a set of option menu items in your application. In addition, it also programmatically checks to see if Google Maps is able to load in your application.

Changing Map Mode

This section shows how to change the Google Maps to display the map in different view modes.

  1. Update MainActivity.java with the following code:

  2. Deploy the application onto a real Android device. Select the Hybrid Mode menu item and observe the map (see Figure 15):

    Hybrid Google Map displayed in Android app
    Figure 15 – Displaying the map in hybrid mode

Displaying Traffic

This section shows how to make Google Maps display traffic conditions.

  1. Add the following statements to the MainActivity.java file:

  2. Deploy the application onto a real Android device. Select the Show Traffic menu item and observe the map (see Figure 16):

    Map with traffic information displayed in Android app
    Figure 16 – Displaying traffic on the map

Zooming In and Out

This section shows how to programmatically zoom in or out of Google Maps.

  1. Add the following statements to the MainActivity.java file:

    So that the full file listing is:

  2. Deploy the application onto a real Android device. Select the Zoom In and Zoom Out menu items and observe the map

Navigating to a Location

This section shows how to make Google Maps display a particular geographical location.

  1. Add the following statements to the MainActivity.java file:

  2. Deploy the application onto a real Android device. Select the Go to a location menu item and observe the map (see Figure 17):

    Map at specific location in Android app
    Figure 17 – Displaying the map of a specific location

Adding a Marker

This section shows how to add a marker (commonly known as a push pin) to Google Maps.

  1. Add the following statements to the MainActivity.java file:

  2. Deploy the application onto a real Android device. Select the Add a Marker menu item and observe the map (see Figure 18):

    Map with marker
    Figure 18 – Displaying a marker

Getting Your Current Location

This section shows how to get Google Maps to find your current location and then move the map to your current location.

  1. Add the following statements to the MainActivity.java file:

  2. Deploy the application onto a real Android device. Select the Get Current Location menu item and then followed by the Show Current Location menu item. Observe the map.

Drawing Lines on the Map

This section shows how to draw a line on Google Maps connecting two different locations.

  1. Add the following statements to the MainActivity.java file:

  2. Deploy the application onto a real Android device. Select the Line connecting 2 points menu item and observe the map (see Figure 19):

    Map with line connecting two points
    Figure 19 – Drawing a line connecting two points

Summary

In this article, you learned how to display and manipulate the new Android Google Maps v2.0 in your Android application. Specifically, you have learned the following:

  • How to apply for a Maps API key
  • How to use the MapFragment to display Google Maps
  • How to change the map mode
  • How to display traffic on the map
  • How to programmatically zoom in and out of the map
  • How to navigate to a location
  • How to add a marker to the map
  • How to obtain your current location
  • How to display your current location
  • How to draw lines on the map

Exclusive tips, how-tos, news and comment

Receive monthly updates on the world of mobile dev.

Other Products

Market leading device intelligence for the web, app and MNO ecosystems
DeviceAtlas - Device Intelligence

Real-time identification of fraudulent and misrepresented traffic
DeviceAssure - Device Verification

A free tool for developers, designers and marketers to test website performance
mobiReady - Evaluate your websites’ mobile readiness

© 2024 DeviceAtlas Limited. All rights reserved.

This is a website of DeviceAtlas Limited, a private company limited by shares, incorporated and registered in the Republic of Ireland with registered number 398040 and registered office at 6th Floor, 2 Grand Canal Square, Dublin 2, Ireland