Configuring Android Applications to Use Google Maps

From RAD Studio
Jump to: navigation, search

Go Up to Mobile Tutorial: Using a Map Component to Work with Maps (iOS and Android)

This topic explains how to configure your Android applications to use the TMapView component and Google Maps Android API.

The following subjects are covered:

  • Getting Android API key
  • Configuring specific project options

Getting Android API key

To access the Google Maps servers with Google Maps Android API, you have to add an Android API key to your application. This section outlines basic steps to get this key:

  1. Retrieve information about the certificate of your application.
  2. Register a project in the Google API Console and enable Google Maps Android API for that project.
  3. Set up Android API key.
Attention: This section describes how to get the Google Maps Android API key using Google API Console. The User Interface of this console, including control names, is subject to change without notice. For information on the recent version of Google API Console, see the appropriate Google documentation.

Retrieving Your Application Certificate

The Maps API key is based on a short form of the digital certificate of your application, known as its SHA-1 fingerprint. The fingerprint is a unique text string generated from the commonly-used SHA-1 hashing algorithm. You may have certificates of two types: Debug certificate and Release certificate.

Note: This section explains how to retrieve the debug certificate. For information on how to retrieve the release certificate, see Get an Android certificate and the Google Maps API key.

To display the debug certificate fingertip

  1. Locate the appropriate debug.keystore file. RAD Studio uses the debug.keystore file located into:
    %Users%\<user name>\AppData\Roaming\Embarcadero\BDS\<version number>\, for example:
    Tip: If you cannot find the debug.keystore file in the above mentioned location, run your application on the Android target platform, and then RAD Studio automatically creates this file.

  2. Find the keytool.exe file. The keytool tool is available in <Java installation folder>\Bin, for example:
    C:\Program Files\Java\jdk1.7.0_71\bin\.

  3. Switch to the folder where the debug.keystore file is located and run:
    keytool -list -v -keystore debug.keystore -alias androiddebugkey -storepass android -keypass android
    Tip: You need to add the full path to keytool; if the keytool.exe file is located in the example provided above, you can use the following:
    "C:\Program Files\Java\jdk1.7.0_71\bin\keytool" -list -v -keystore debug.keystore -alias androiddebugkey -storepass android -keypass android

  4. The keytool tool displays an output similar to this: Alias name: androiddebugkey Creation date: Jan 24, 2015 Entry type: PrivateKeyEntry Certificate chain length: 1 Certificate[1]: Owner: CN=Unnamed, OU=Mobile Organization, O=My Company, L=San Francisco, ST=California, C=US Issuer: CN=Unnamed, OU=Mobile Organization, O=My Company, L=San Francisco, ST=California, C=US Serial number: 1af9d77f Valid from: Sat Jan 24 23:12:12 GST 2015 until: Wed Jun 11 23:12:12 GST 2042 Certificate fingerprints: MD5: 20:E1:7D:12:4F:83:2A:37:FB:F5:43:3B:CC:27:88:4B SHA1: 0B:4D:1E:83:2F:91:3A:BD:92:EB:87:62:6D:34:66:9D:6A:51:3B:ED SHA256: 91:75:AC:BE:C9:74:6F:67:3E:9A:E4:35:04:D4:05:E0:8F:A4:54:30:41:4A:EB:0A:61:E0:D1:33:7C:D8:0F:82 Signature algorithm name: SHA256withRSA Version: 3 The line that begins with SHA1 contains the certificate's SHA-1 fingerprint. The fingerprint is the sequence of 20 two-digit hexadecimal numbers separated by colons.

Creating an API Project in the Google API Console

Attention: Before proceeding with this section, make sure that you have a Google Account.
  1. Go to the Google API Console.
  2. From the projects list, select Create a project.
  3. Enter a name and specify a project ID or accept the default.
  4. Click Create.

Setting up an Android API Key

Now you can set up an Android API key. It is possible to register more than one key per project.

To set up an Android API key

  1. Navigate to your project in the Google API Console.
  2. Go to the API Manager page, and on the left, choose Credentials.
  3. Click Create credentials and then select API key.
  4. In the Create a new key page, click Android key.
  5. In the resulting dialog, click the + Add package name and fingerprint button, and then enter the appropriate information in Package name and SHA-1 certificate fingerprint.
    Attention: To get a valid Android API key, ensure that you use the exact application package name. This name uniquely identifies your application. The package key in the Android Version Information contains the package name (see the screenshot under the "To set the apiKey key value" procedure later in this document).
  6. Click Create.
    Google API Console displays the API key window with a forty-character API key, for example: API key: AIzaSyCMuJ2w8_gQEedvqUg-9lxWXFg7feAl9PQ

Configuring Specific Project Options

For your Android application to access Google Maps, ensure that you have configured the following project options and permissions:

Attention: Before proceeding with this section, in the Projects Window, set the Target Platform to Android:
ProjectManager myMap.png

To check the configured permissions list

  1. In the RAD Studio IDE, open Project > Options > Uses Permissions
  2. Ensure that you have enabled the following permissions:
    • Access coarse location
    • Access fine location
    • Access network state

    Android Uses Permisiions.png

To check the Entitlement List

  1. In the RAD Studio IDE, open Project > Options > Entitlement List
  2. Ensure that the Maps Service option is enabled:

To set the apiKey key value

  1. In the RAD Studio IDE, open Project > Options > Version Info
  2. Set the apiKey key to the Android API key value that you have requested for your application (see Step 6 in the Setting up an API Key section earlier in this document).
  3. Click OK.
    Android apiKey.png
Attention: For your changes to the project options to take effect, press Shift + F9 to rebuild your project.

See Also

Code Samples