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
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.
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 KeyBefore 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.
- Copy the MD5 fingerprint and go to http://code.google.com/intl/fr-FR/android/maps-api-signup.html .Follow the instructions in the page to obtain the Google Maps API key.
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>
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"
/>
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);
Comments
Post a Comment