Always On, Always Connected Mapping

Mapping

Android, being intimately linked with Google, offers very robust integration with Google Maps.

In order to use Google Maps in Android, we have to use an external library. Although the Mapping capabilities are included in Android, to use them as a developer requires a few extra steps.
These steps are outlined here: http://code.google.com/android/add-ons/google-apis/

In short, we need to procure a Maps API Key for development (the process is a bit different when we are ready to distribute the application).

Using the debug/development key provided by the SDK we can generate a request for an API key.

First we hash the debug key (which on mac and linux is located here: ~/.android/debug.keystore)

keytool -list -alias androiddebugkey \
-keystore ~/.android/debug.keystore \
-storepass android -keypass android

which outputs (for me):

Certificate fingerprint (MD5): C8:9E:82:75:AE:AB:D2:72:A2:EE:7D:D2:33:BE:89:02

(Note: This might be a bit tricky on a PC. You may have to find the keytool.exe application which will be installed somewhere along with the JDK. When you run it, you’ll probably have to add the -v flag as well. Ignore everything but the MD5 fingerprint.

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

)

We paste that fingerprint into the form on this page:

https://developers.google.com/maps/documentation/android/v1/maps-api-signup

which spits back a key that can be used in the app when using the MapView view.

<com.google.android.maps.MapView
	android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:apiKey="YOUR API KEY"
/>

or

MapView mMapView = new MapView(this, "YOUR API KEY");

The application will also have to specify that it is using the maps 3rd party library in the application tag in the manifest:

   <uses-library android:name="com.google.android.maps" />

Here is an example: (Don’t forget to use an Emulator that includes the 3rd party APIs)

package com.mobvcasting.maptest;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class MapTest extends MapActivity implements LocationListener {

	LocationManager lm; 
	MapView mv;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
		lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 6000l, 5.0f, this);
        
		mv = (MapView) this.findViewById(R.id.mapview);
    }

    public void onPause() {
		super.onPause(); 
		lm.removeUpdates(this);
	}

	public void onLocationChanged(Location location) {
		MapController mc = mv.getController();
		
		GeoPoint p = new GeoPoint((int) (location.getLatitude() * 1000000), (int) (location.getLongitude() * 1000000));
		mc.animateTo(p);
		//http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/MapController.html#setZoom(int)
		mc.setZoom(14);
	}

	public void onProviderDisabled(String provider) {
		
	}

	public void onProviderEnabled(String provider) {
		
	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}

res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<com.google.android.maps.MapView
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:apiKey="0Pr8kowSuIxjgvMYb7_ZWv0wWHjxVUZoyRGUGBA"
	android:id="@+id/mapview" />    
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.mobvcasting.maptest"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MapTest"
                  android:label="@string/app_name">
            <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" />
    </application>
    <uses-sdk android:minSdkVersion="8" />
	<uses-permission android:name="android.permission.INTERNET"></uses-permission>
	<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
	<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
</manifest> 
If we want to have the default controls show up on our MapView, we simply have to issue this command:
mv.setBuiltInZoomControls(true); 
Assuming that "mv" is the MapView object.

Good tutorial: https://developers.google.com/maps/documentation/android/v1/hello-mapview