Android and Google Maps

Working with Maps

Part 1 : How to get a debug key in order to use the map

Navigate to your .android folder (mine is located in the home folder) open a console and type:

cd .android

Command to obtain the debug key :

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

this is the result you will get :


androiddebugkey, 2 avr. 2011, PrivateKeyEntry,

Empreinte du certificat (MD5) : 23:7E:ED:C6:55:5D:E3:7E:A9:1A:56:3F:84:52:4E:6A

So now grab your key similar to : 23:7E:ED:C6:55:5D:E3:7E:A9:1A:56:3F:84:52:4E:6A

Now go here and fill out the form. You will get something like :

06C2OsQX3QcIlUryKWf7bHlarpF4q16V_uw46gw

Now you can test the MapViews in your emulator. This is just the debug key and will not allow you to publish your apps on the Google Market.

Part 2 : Geo-location and the emulator

In order to use the GPS functions in the emulator you need to do the following :

open a console and type :

telnet localhost 5554

and then type

geo fix -106.977541 40.600608

Replace with the coordinates you want your emulator to have.

Part3 : Showing a MapView

1. Create an android project in eclipse, it has to be a Google API project
2. Replace the main.xml code by this one :

<?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" android:id="@+id/mainlayout">

<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="06C2OsQX3QcIlUryKWf7bHlarpF4q16V_uw46gw"
/>
<LinearLayout android:id="@+id/zoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>

As you can see the MapView has to be placed in a relative layout. The API key is where you place your own key otherwise the map will not work.

Here are the permissions to add to the android Manifest :

<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>

And here is the code of the Activity with the comments inside :

public class MyMapActivity extends MapActivity implements LocationListener {
private MapView mapView;
private LocationManager locationManager;
private String provider;
private Location location;
private String lat;
private String lon;
private MapController mapController;
private AlertDialog alertDialog;
private MyLocationOverlay me2;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mapView = (MapView) findViewById(R.id.mapView);

// these line will show the zoom
mapView.setBuiltInZoomControls(true);

mapView.setStreetView(true);

// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

// Define the criteria how to select the location provider
Criteria criteria = new Criteria();
// gettting an available provider for example internet or GPS
provider = locationManager.getBestProvider(criteria, true);

try {
locationManager.requestLocationUpdates(provider, 0, 0, this);

location = locationManager.getLastKnownLocation(provider);
// lets try to get a conection

double lat1 = location.getLatitude();
double lng1 = location.getLongitude();
mapView.getController().setCenter(
getPoint(location.getLatitude(), location.getLongitude()));

} catch (Exception e) {

// if no success lets whow some alert dialog that the providers are
// unavailable

if (!locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
buildDialog(R.string.noGPS);
}

// localizacion por internet
else if (!locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
buildDialog(R.string.noInternet);
}

// if no location lets set up some random location just to show the
// map
mapView.getController().setCenter(
getPointfromStrings("40.600608", "-106.977541"));
}

mapController = mapView.getController();
mapController.setZoom(16); // Zoom 1 is world view

mapView.invalidate();

loadMap();

}

public void loadMap() {

Drawable marker2 = getResources().getDrawable(R.drawable.marker);
marker2.setBounds(0, 0, marker2.getIntrinsicWidth(), marker2
.getIntrinsicHeight());

// let's create an overlay in order to show our location
mapView.getOverlays().add(new SitesOverlay(marker2));

try {
me2 = new MyLocationOverlay(this, mapView);
} catch (IllegalArgumentException il) {
il.printStackTrace();

}
mapView.getOverlays().add(me2);
mapView.invalidate();
}

protected void buildDialog(int id) {

alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Map Activity");

alertDialog.setMessage(getResources().getString(id));

alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// here you can add methods

dialog.dismiss();

}

});

alertDialog.setIcon(R.drawable.icon);
alertDialog.show();
}

// two methods to get Geopoints
private GeoPoint getPoint(double lat, double lon) {
return (new GeoPoint((int) (lat * 1000000.0), (int) (lon * 1000000.0)));
}

private GeoPoint getPointfromStrings(String lat, String lon) {
return getPoint(Double.parseDouble(lat), Double.parseDouble(lon));
}

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

@Override
public void onLocationChanged(Location location) {
}

@Override
public void onProviderDisabled(String provider) {

}

@Override
public void onProviderEnabled(String provider) {

}

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

}

private class SitesOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> items = new ArrayList<OverlayItem>();

public SitesOverlay(Drawable marker) {
super(marker);
boundCenterBottom(marker);

try {
items.add(new OverlayItem(new GeoPoint((int) location
.getLatitude(), (int) location.getLongitude()), "I am",
"Here"));

} catch (AndroidRuntimeException an) {
an.printStackTrace();

}

catch (Exception e) {
e.printStackTrace();

}
// very important otherwise the app will terminate with errors
populate();
}

@Override
protected OverlayItem createItem(int i) {
return (items.get(i));
}

// a toast when we click on the marker
@Override
protected boolean onTap(int i) {
Toast.makeText(MyMapActivity.this,
items.get(i).getSnippet() + "\n" + items.get(i).getTitle(),
Toast.LENGTH_LONG).show();

return (true);
}

@Override
public int size() {
return (items.size());
}
}

}

Enjoy!

As you can see the MapView has to be placed in a relative layout. The API key is where you place your own key otherwise the map will not work.