Android - API Google Maps - Using Google Maps API v2

Google made a new version of its famous Google Maps API, called Google Maps API v2.

Things are new, such as the OpenGL ES version 2.

But let's try to create an application, in this Google Maps API v2 tutorial for Windows 7 and less or more.

The result of this tutorial can be found in the Google Play store as an application, check for Tuto 6.

Google Play Services library

Your Android SDK Manager has to be updated.

Especially you need to install or update: Extras > Google Play Services.

Once downloaded, you should have it on your computer.

For example, on mine I could find it there:

  • C:\soft\adt-bundle-windows\sdk\extras\google\google_play_services\libproject\google-play-services_lib

So, in your Eclipse ADT, click:

File > Import > Existing Android Code Into Workspace > Browse >
C:\soft\adt-bundle-windows\sdk\extras\google\google_play_services\libproject\google-play-services_lib

Still in the Import Projects windown, do not forgot to check Project to Import if it's not already done and above all check Copy projects into workspace, then click Finish.

A new project has appeared in the Package Explorer, named google-play-services_lib.

We have now to link this library with our project.

So let's create a new Android Application.

Creating a new Android project

  • Application Name: MyGoogleMapsAPIv2
  • Project Name: MyGoogleMapsAPIv2
  • Package Name: com.badprog.mygooglemapsapiv2
  • Min SDK: API 8
  • Target SDK: API 17
  • Compile With: API 17

Click Next > Next > Next > Next > Finish.

A very classic way to create an application but let's do easy, it's better to understand.

Linking the library with our project

Right click the MyGoogleMapsAPIv2 project > Properties > Android. At bottom there is a Library part > Click Add... > google-play-services_lib > OK > OK.

Done.

We need a key

Why?

In order to verify that the person who try to modify your code is still the same person that had created it  before.

You could change the location of this debug key by changing the path until it by doing from Eclipse/ADT > Windows > Preferences > Android > Build.

But for this tutorial I suggest you to let all by default.

Indeed, by default you have a debug key. Yet with it you could only see your Google Maps application on your computer.
If you would like to upload it in the Google Play store, you have to use a release key.

But before using a release key, let's try to use the debug key.

This key is automatically created in:

  • C:\Users\YOUR_NAME\.android

Inside this folder there is the debug.keystore key.

So to use this key, we have to come back to the directory of your name:

  • C:\Users\YOUR_NAME

Select the .android folder with one single left click.
Then press SHIFT + Right click and in the menu window select Open a command line here.

A black window appears, it's what we call a shell.

There write it down the following command :

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

You should see some lines with letters and numbers.

Find the line that begins by SHA1, like this one:

  • SHA1 : 84:BD:28:73:1B:6F:C2:84:2C:8D:03:11:56:19:E4:KD:38:C3:80:73

It's what we call a certificate fingerprint.

At this point, we have to take this SHA1 certificate fingerprint and transform it into a real key to put in our XML code.

And for that it's time to go to the Google APIs website.

Google APIs Console

Go now to this website: https://code.google.com/apis/console/ and create a project.

To that end, on the left, there is a combobox with as first choice API Project.

Just click it and > Create > You could here change the name > Create project.

Your first project for the Google APIs is now created.

On the left, click Services and, in the list, activate Google Maps Android API v2.

You should have the Google Maps/Google Earth APIs Terms of Service on the screen.

Accept it if you agree with terms.

The status of the Google Maps Android API v2 is now in green with ON in white.

OK, let's now create a new Android key.

On the menu, on the left, click API Access.

A new web page appears.

At the bottom (or near), click Create new Android key...

A pop up appears.

In the textarea enter your SHA1 certificate fingerprint (previously created).

Copy and paste it than at the end, write a semi colon and the package of your Android application.

For example, we could have:

  • SHA1                 --> 84:BD:28:73:1B:6F:C2:84:2C:8D:03:11:56:19:E4:KD:38:C3:80:73
  • semi colon       --> ;
  • package name --> com.badprog.mygooglemapsapiv2

And finally what you have to write into the textarea is:

  • 84:BD:28:73:1B:6F:C2:84:2C:8D:03:11:56:19:E4:KD:38:C3:80:73;com.badprog.mygooglemapsapiv2

Click then Create.

A new part appears in the API Access web page and you should see the API key generated that is something like this:

  • WSzxbPyBxPapkSGwWHy0rN_tUbrstH7Caaa8wNHM

Come back to the Android application and Eclipse/ADT

Now open your AndroidManifest.xml (at the root of your project) and make modification to have exactly the same file as the following one (don't forget to change the meta-data value by your own API key):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.badprog.mygooglemapsapiv2"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    
    <permission
            android:name="com.badprog.mygooglemapsapiv2.permission.MAPS_RECEIVE"
            android:protectionLevel="signature"/>
        <uses-permission android:name="com.badprog.mygooglemapsapiv2.permission.MAPS_RECEIVE"/>


           <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    
        
        <uses-feature
            android:glEsVersion="0x00020000"
            android:required="true" />
    
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
                
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="WSzxbPyBxPapkSGwWHy0rN_tUbrstH7Caaa8wNHM"/>          
        
        <activity
            android:name="com.badprog.mygooglemapsapiv2.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Now the MainActivity.java:

package com.badprog.mygooglemapsapiv2;

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

public class MainActivity extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

And finally the activity_main.xml:

<?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"/>

Notice that you could, of course integrate this fragment into a Relative or Absolute layout.

Now our application, google play services and the debug key are ready.

Let's try it with the debug keystore > Right click your project > Run As > Android Application.

You should see your application opens with a beautiful Google Maps!

But it was a first step.

If you would like to upload your application on the Google Play store, you have to sign this application with a release key.

Generating a release key

Before signing your application for the Google Play store, it's necessary to generate a release key.

Unlike the debug key, the release one can be generated anywhere you want.

For this example, we will create a new directory especially for our release key.

This directory will be mykey in the following path:

  • C:\soft\adt-bundle-windows\mykey

In order to generate our key, we have to use the keytool command that have certainly installed without knowing it.

Indeed, when you installed your JDK, you also installed it.

So open the following folder:

  • C:\soft\adt-bundle-windows

Left click the mykey directory once and press SHIFT + Right click to open the window menu and select Open a window command here.

In the following command we will use:

  • release.keystore as a release key name
  • myreleasekey as alias name
  • RSA as cryptography algorithm
  • 2048 for the length of this algorithm
  • 200000 as number of days (547 years) for the validity of this key (need at least 10000)

In the black window, type then this command line:

keytool -genkey -v -keystore release.keystore -alias myreleasekey -keyalg RSA -keysize 2048 -validity 1000000

Keytool is now asking you to enter a password, retype it.

And now you have to answer 6 questions.

The seventh asks you if you agree with what you've just said.
Type yes then press ENTER.
Notice that you have to write yes in the language in which the questions are.
For example, if you are French, you have to answer oui instead of yes, otherwise it won't work and will ask you for ever the same questions.

Keytool will now generate the RSA key for you and ask if you would like to create a new password for the alias.
If you would like to keep the same password as the release.keystore, just type ENTER.
If you would like to add a new password for the alias, type it.

Your release key is now generated!

If you would like to see your SHA1 release fingerprint certificate, you'll have to type the following command line in the C:\soft\adt-bundle-windows directory:

keytool -list -v -keystore mykey\release.keystore -alias myreleasekey

Exporting the project as a APK file

In order to be signed, your application has to be converted into a APK format.

But this export must be unsigned first.

With the JDK 7, there is an error when you try to export an unsigned project.
This error is appears with a popup window:

Export Aborted
Export aborted because fatal lint errors were found. These are listed in the Lint View. Either fix these before running Export again, or turn off "Run full error check when exporting app" in the Android > Lint Error Checking preference page.

The error could be too into Eclipse/ADT:

"auth_client_availability_notification_title" is not translated in af, am, ar, be, bg, ca, cs, da, de, el, en-rGB, es, es-rUS, et, fa, fi, fr, hi, hr, hu, in, it, iw, ja, ko, lt, lv, ms, nb, nl, pl, pt, pt-rBR, pt-rPT, ro, ru, sk, sl, sr, sv, sw, th, tl, tr, uk, vi, zh-rCN, zh-rTW, zu

Issue: Checks for incomplete translations where not all strings are translated
Id: MissingTranslation

If an application has more than one locale, then all the strings declared in one language should also be translated in all other languages.

If the string should not be translated, you can add the attribute translatable="false" on the <string> element, or you can define all your non-translatable strings in a resource file called donottranslate.xml. Or, you can ignore the issue with a tools:ignore="MissingTranslation" attribute.

By default this detector allows regions of a language to just provide a subset of the strings and fall back to the standard language strings. You can require all regions to provide a full translation by setting the environment variable ANDROID_LINT_COMPLETE_REGIONS.

You can tell lint (and other tools) which language is the default language in your res/values/ folder by specifying tools:locale="languageCode" for the root <resources> element in your resource file. (The tools prefix refers to the namespace declaration http://schemas.android.com/tools.)

[]

To remove this error, go to > MyGoogleMapsAPIv2 > res > values > strings.xml.

Within, add everywhere there is a <string> tag, the following attribute and its value:

  • translatable="false"

So that before you had:

    <string name="app_name">MyGoogleMapsAPIv2</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>

And now you have:

    <string translatable="false" name="app_name">GM1</string>
    <string translatable="false" name="action_settings">Settings</string>
    <string translatable="false" name="hello_world">Hello world!</string>

If errors are still there, click Project > Clean > (select your project name) > OK.

Do the same with the google-play-services_lib project that you previously created and then modify the following file from Eclipse/ADT:

  • google-play-services_lib >res > values > strings.xml

At lines 59 to 68, there are also <string> tag to modify, so before you had:

  <!-- Auth client code resources (prefix with auth_client --><skip />
  <!--  Title for notification shown when GooglePlayServices is unavailable [CHAR LIMIT=70] -->
  <string name="auth_client_availability_notification_title">Install/Update/Enable Google Play services</string>

  <!--  Title for notification shown when GooglePlayServices is unavailable [CHAR LIMIT=42] -->
  <string name="auth_client_play_services_err_notification_msg">Google Play services error</string>

  <!--  Requested by string saying which app requested the notification. [CHAR LIMIT=42] -->
  <string name="auth_client_requested_by_msg">Requested by <xliff:g id="app_name">%1$s</xliff:g></string>
  <!-- End Auth client resources --><skip />

Now you have:

  <!-- Auth client code resources (prefix with auth_client --><skip />
  <!--  Title for notification shown when GooglePlayServices is unavailable [CHAR LIMIT=70] -->
  <string translatable="false" name="auth_client_availability_notification_title">Install/Update/Enable Google Play services</string>

  <!--  Title for notification shown when GooglePlayServices is unavailable [CHAR LIMIT=42] -->
  <string translatable="false" name="auth_client_play_services_err_notification_msg">Google Play services error</string>

  <!--  Requested by string saying which app requested the notification. [CHAR LIMIT=42] -->
  <string translatable="false" name="auth_client_requested_by_msg">Requested by <xliff:g id="app_name">%1$s</xliff:g></string>
  <!-- End Auth client resources --><skip />

Now your application can be exported as unsigned:

> Right click your project > Android Tools > Export Unsigned Application Package...
> As file name choose MyGoogleMapsAPIv2-1.apk > Save.

Be sure to have export your application in the export directory.

It's time now to sign the application.

Signing the application

Go in the adt-bundle-windows directory.

Open an MSDOS shell and write it down:

jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore mykey\release.keystore export\MyGoogleMapsAPIv2-1.apk myreleasekey

You are asking to write your release.keystore password.

Then to enter your myreleasekey alias password.

Once done, every single file of your application will be signed by Jarsigner.

This is not the end!

We have to proceed another task, make our application memory optimized.

Still in the shell and in the adt-bundle-windows directory, let's use the Zipalign tool.
This tool could be found in the C:\soft\adt-bundle-windows\sdk\tools directory.

So just execute this command line to optimize the MyGoogleMapsAPIv2-1.apk into another file MyGoogleMapsAPIv2-2.apk from the C:\soft\adt-bundle-windows directory:

sdk\tools\zipalign.exe -v 4 export\MyGoogleMapsAPIv2-1.apk export\MyGoogleMapsAPIv2-2.apk

I chose to create two different files in order to better understand what we are going to do.

Important note

If you were sure to have the right API key and that you had the following error:

Failed to load map. Error contacting Google servers. This is probably an authentication issue (but could be dur to network errors).

Just clear data on your mobile phone and uninstall it.
Then resend your application on your mobile.
It should work.

Don't forget to use the debug key if you are testing your application directly on your computer.
The release key is only when you are ready to send it on Google Play store.

Conclusion

We have now the MyGoogleMapsAPIv2-2.apk ready to be uploaded in the Google Play store.
Well done, you've just made it. cool

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.