Android: a Google Map in your Android SDK app

  • user warning: Table './armyof7_armyofdata/d6_sessions' is marked as crashed and should be repaired query: SELECT COUNT(sid) AS count FROM d6_sessions WHERE timestamp >= 1411131305 AND uid = 0 in /home/armyof7/public_html/includes/session.inc on line 157.
  • user warning: Table './armyof7_armyofdata/d6_sessions' is marked as crashed and should be repaired query: SELECT COUNT(DISTINCT s.uid) FROM d6_sessions s WHERE s.timestamp >= 1411131305 AND s.uid > 0 in /home/armyof7/public_html/modules/user/user.module on line 801.

Using a Google Map within an Android SDK application requires the programmer to be registered with Google and use a specific API key to access the map -as the data (map tiles) belongs to them, they want to control the usage. Fair enough.

Your application must use a MapView component to display the map -this is a special View that retrieves and displays the map tiles from Google.

The process of registration for a Map key is free and not too hard to complete -read http://code.google.com/android/add-ons/google-apis/mapkey.html for more on this process.

You can register as many keys as you need.

A bundle of hints

In creating a simple native version of Ground Zero for Android, we noticed the following caveats:

1) You need two sets of API keys: one key for developing (called the "debug.keystore") and one key for releasing your application live into the world.

To make things more interesting (and confusing), the "debug" key will not work if you compile on a connected physical device - it only works when running in the software emulator that comes with the Android SDK.

This simple mistake meant we wasted a couple of hours looking for solutions to properly display the map in our device.

2) Your application needs to have the right AndroidManifest.xml: here's where you assign permissions and other goodies to your application -not giving the right permissions, or having an incorrect XML structure- will prevent the maps from loading. 

Here's a functional AndroidManifest that can be copied and used as basis for your own (copy and paste into your own):

<?xml version="1.0" encoding="utf-8"?>
  <manifest package="{replace with your qualified package}"
    android:versionCode="1"
    android:versionName="1.0" 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-sdk android:minSdkVersion="4" /> 
    <application android:icon="@drawable/icon"
                 android:label="@string/app_name" 
                 android:debuggable="true">   
      <activity android:name=".{replace with name of your main Activity}"
                android:label="@string/app_name"
                android:theme="@android:style/Theme.NoTitleBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>
      <uses-library android:name="com.google.android.maps" ></uses-library>
    </application>
</manifest>

You must replace the items between curly brackets with you own values. In our environment, the value for package is com.carloslabs.android.groundzero, and our activity name is .GroundZeroActivity (with a period at the beginning)

3) You must have a valid API key in your main.xml layout: an example file would look like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">
    <com.google.android.maps.MapView         android:id="@+id/mapView"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:enabled="true"         android:clickable="true"         android:apiKey="0qQ_--3XSRmTmmcyF-778886666666666666666"        /> </RelativeLayout>

You must use your own Google generated Map API key (the one in the file is bogus and will not work) - if you are running your app in the software emulator, choose your "debug.keystore". If you are running in a physical Android device use your production keystore.

4) Make sure you link to the right library: not all Android machines can use the Google Maps library; it needs to be linked into your projects before you can use the objects and components (like MapView)

Go here to learn more: http://code.google.com/android/add-ons/google-apis/maps-overview.html

Get started on Maps

We have published an empty stub for the upcoming Ground Zero for Android, a native version of the ever-popular nuclear bomb mapping resource.

The stub just loads a Map and displays a MapView on an Android device - it does not contain any code for the actual Ground Zero application. It works both in the emulator and in a physical Android device. You may use it as a basis for your own application.

You only need to provide your own Map API key (see item 3). If you decide to rename the project, you must also see item 2 above.

The archive can be retrieved from here http://www.carloslabs.com/files/GroundZero_stub.zip

- - -

Thanks to the community at stackoverflow and to this very useful page at mobiforge.

Comments

Google maps API

I was curious about how the app developers got the access to use Google maps. I'd imagine the same thing goes for Apple's iPhone as well?

Does the programmer or company have to pay any royalty fees for using Google map's APIs? I'm interested because I'd like to have our team of devs and designers start programming apps for mobile platforms. Any information would be greatly appreciated.

Diseño Web Perú

El acceso es gratuito

El acceso a las herramientas de Google Maps es gratuito - el unico requerimiento para un desarrollador es registrarse en Google.

El uso tambien es gratuito, siempre que la aplicacion no sea comercial.

Pueden empezar por entrar al website de Code.Google para saber mas sobre programacion con Google Maps

http://code.google.com/apis/maps/index.html

suerte, Carlos