Show Google Map on your android app.

     Showing Google map on your android application,you can make many useful application like gps tracker,navigation systems etc.But how to show Google map in your application?So follow this tutorial to get the Google map in some easy steps.

      First of all,make a project in your favorite IDE(this tutorial is based on Eclipse IDE).Here the important think is set your application build target as Google APIs.Otherwise you will not be able to use MapView widget.

      Now open the main.xml in layout folder.There you don't get some MapView widget that can be dragged & dropped like the other widgets(Button,EditText etc.). You have to write xml code for the MapView. Now lets check the code...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <com.google.android.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
        android:apiKey="Your Android Maps API key"
    />
</LinearLayout>



        Now go to your "src" folder and open the java file(Created default by the Android ADT there). There you can see the java file extends Activity class.Now delete the "Activity" and in place of that write "MapActivity".You have to import "com.google.android.maps.MapActivity" for this.Now initialize the MapView object.Now check the code with yours below:
package com.android.googlemap;

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

import android.os.Bundle;

/**
 * @author www.androidcookers.co.cc
 * {@link www.androidcookers.co.cc}
 *
 */

public class GoogleMapActivity extends MapActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        @SuppressWarnings("unused")
  MapView mp;
        
        mp=(MapView)findViewById(R.id.mapview);
    }

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

      You have to add internet permission and uses-library android:name="com.google.android.maps" to the manifest xml. Now check the manifest code:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.googlemap"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <uses-library android:name="com.google.android.maps" />
        <activity
            android:label="@string/app_name"
            android:name=".GoogleMapActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

      Now if you run the application,you will see something like this.Here the question is where is the map?Actually to show the map you need a Android Maps API key.For this you have to follow some more steps(Relax,this time not in your java file).

      You may be noticed that there is a default debug key(created by the Android ADT). You can check the debug key location in your Eclipse IDE by going to Window->Preferences->Android->Build.

      Now you have to generate MD5 fingerprint for the debug key. For this you have to open the dos window(Command Prompt). You will find a file keytool.exe in your java installation directory. You have to copy the keytool path. Now go to the keytool path by the dos command "cd" like this:[C:\Program Files\Java\jdk1.6.0_16\bin>keytool].The underlined section is your specific keytool path.Now to get the MD5 fingerprint by the following command:

C:\Program Files\Java\jdk1.6.0_16\bin>keytool -list -alias androiddebugkey -keys
tore "Your debug key location" -storepass android -keypass android

        Your debug key location can be found by going to eclipse>windows>preferences>android>build.
        The underlined section will be your machine specific.Now you get the MD5 fingerprint like that:
6D:44:55:DD:25:97:2E:3C:EA:A9:89:02:44:6D:F5:0E .Yours will be different of course.

        Now go to the site https://developers.google.com/android/maps-api-signup.There you have to submit the MD5 fingerprint.You have to sign-up to your Google account for that.Now you will get your Android Maps API key. Now copy the key and paste it in your xml like this.
android:apiKey="Your-api-key".

        Now run the application again and this time the map will load successfully.

1 comment: