lundi 31 janvier 2011

Create your first Google Maps application

In this article i will show you  the different steps to follow to create your first Google Maps application.
We will also demonstarte some usefull options that  give your application more flexibility and performance.

Creating the Project
Using Eclipse, create a new Android Project and name it  MapsDemo as  shown in Figure 1

Figure 1
Obtaining a Maps API Key
Before you integrate Google Maps in your Application, you need  to apply for a Free  Google Maps API Key.
To apply for key follow the steps  below.
You can refrence to Google's documentation for more  details.

  • First, find this file (debug.keystore)  and copy it (for simplicity) in c:\\Android(Create a folder named Andrioid under c:\\)
  • Extract the MD5 fingerpoint using the keytool.exe included with your JDK installation.You can find the keytool.exe under "C:\Program Files\Java\<JDK_version_number>\bin" folder.
keytool.exe -list -alias androiddebugkey -keystore "C:\android\debug.keystore" -storepass android -keypass android





Modify the  AndroidManifest.xml
Open The  AndroidManifest.xml file and add the following as a child of the <application> element.

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

You also need access to the Internet to retreive map tiles,so you must also request teh INTERNET permission.In the manifest file, add teh following as a child of the <manifest> element.

<uses-permission android:name="android.permission.INTERNET" />

The AndroidManifest.xml file :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="net.mapsdemo.android"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <uses-library android:name="com.google.android.maps" />
        <activity android:name=".MapsActivity"
                  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>
 <uses-permission android:name="android.permission.INTERNET" />
</manifest>
Dispalying the Map
To display the Google Maps in your Android application, modify the main.xml file located in the res/layout folder. You shall use the <com.google.android.maps.MapView> element to display the Google Maps in your activity. In addition, let's use the <RelativeLayout> element to position the map within the activity:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:apiKey="9B:AE:A4:18:D6:16:C9:9D:FB:99:E2:84:6B:A8:13:F3"
/>
N.B : android:apiKey : you have to set Your Maps Key.
In the MapsActivity.java file, modify the class to extend from the  MapActivity  class, instead of the normal Activity class:

package net.mapsdemo.android;
import com.google.android.maps.MapActivity;


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


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


 @Override
 protected boolean isRouteDisplayed() {
  // TODO Auto-generated method stub
  return false;
 }
}

That's it !! Your first Google Maps application looks like following :


Zoom, Sreet Maps ...
There's a very simple zom feature built into MapView class, which you can summon with setBuiltInZoomControls(boolean).Do this at the end of the onCreate() method.

MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);

You can also display the map in Street View, using the streetView(boolean) method.

mapView.setStreetView(true);



Aucun commentaire:

Enregistrer un commentaire