Google Map in Android

Introduction

This is an introduction to the map activity in Android. In this article I will explain how add a map application to your Android mobile. To add a map application to Android you first must get a MD5 fingerprint for your PC and use the fingerprint to get a Google maps Android API Key from developer.google.com.

Then we extend the MapActivity class from "com.google.android.maps.MapActivity" as described in the following instructions.

Step 1

Create a new Android project as "File" -> "New" -> "Android Application Project" as shown in the following image. For every map application select a target that supports Google APIs.

mymapNew.jpg

Step 2

Now open the XML file "res/layout/activity_main.xml" and update it with the following code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

   <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="0UPSNoPt1QRgh5ctoV_t1vbo2lRHQbM2AsnEMeQ"

/>

</RelativeLayout>

Step 3

Open the Java file as "MainActivity.java" and update it with the following code:

package com.example.myfirstmap;

import android.os.Bundle;

import android.view.Menu;

import com.google.android.maps.MapActivity;

public class MainActivity extends MapActivity {

      @Override

      protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

             setContentView(R.layout.activity_main);

            /*WebView ww = (WebView)findViewById(R.id.mWebView);

            ww.loadUrl("http://www.google.com");*/

      }

      public boolean onCreateOptionsMenu(Menu menu) {

            // Inflate the menu; this adds items to the action bar if it is present.

            getMenuInflater().inflate(R.menu.main, menu);

            return true;

      }

      @Override

      protected boolean isRouteDisplayed() {

            // TODO Auto-generated method stub

            return false;

      }

}

 

Step 4

Open the "AndroidManifest.xml" file and update it with the following code:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.myfirstmap"

    android:versionCode="1"

    android:versionName="1.0" >

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="17" />

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

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

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

        <activity

            android:name="com.example.myfirstmap.MainActivity"

                 android:theme="@android:style/Theme.NoTitleBar"

            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>

</manifest>

Step 6


The output is as shown in the following image:

map.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all