Wednesday, November 23, 2011

Using Google Maps in Android

In this post I show how to create a Google Maps in your application.


Create a New project




















Select BuildTarget as GoogleAPIS 2.3.3 version























 Enter Package name and click Finish.






To use google maps in our application we need a MD5 finger print provided by
the google. To get the MD5 finger print follow the steps.

Right click on your project and click "Android Tools> Export Signed Application
Package..."

  



It opens a window "Export Android Application". Click Next. Choose "Create new keystore"
and browse for the location where you can store the generated key.

Note: Remember the location.
Enter password and confirm. Click Next.




















Enter Alias, password,confirm,validity and First and Last Name
Click Next




















Browse for the location to save the .apk file.
Click Finish.





















Open command prompt and change your directory to Java's bin directory.












Enter the following command
bin> keytool -list -alias Aliaskey(enter your alias key name here) -keystore "enter your
keystore location in double quotes" -storepass yourpassword -keypass yourpassword

Enter









It generates a MD5 finger print.









copy the generated finger print without any spaces.

Open the following link in browser.

http://code.google.com/android/maps-api-signup.html

paste your fingerprint code and click Generate API key.
















I
It generates a code for mapview as follows
<com.google.android.maps.MapView
                android:id="@+id/map_view"
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
                 android:enabled="true"
                 android:clickable="true"
                 android:apiKey="enter your key here"
                 />
 copy this and paste in main.xml file.


main.xml

<?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/map_view"
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
                 android:enabled="true"
                 android:clickable="true"
                 android:apiKey="your key here"
                 />

</LinearLayout>


To run this application we need to add the permission in AndroidManifest.xml file. And also include the library file.


AndroidManifest.xml

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

    <uses-sdk android:minSdkVersion="10" />
    <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=".GoogleMapsActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

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

</manifest>


GoogleMapsActivity.java


import android.os.Bundle;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

public class GoogleMapsActivity extends MapActivity {
    private MapView mapview;
    private MapController controller;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        mapview=(MapView)findViewById(R.id.map_view);
        mapview.setSatellite(true);
       
    }
    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
}

Before run your project we need to create a virtual device for running google maps.





















To start the emulator select the vertual device and click start button.



















Now run your project and you will get the following out put.


No comments:

Post a Comment