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 an 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:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.    xmlns:tools="http://schemas.android.com/tools"  
  3.    android:layout_width="match_parent"  
  4.    android:layout_height="match_parent"  
  5.    android:paddingBottom="@dimen/activity_vertical_margin"  
  6.    android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.    android:paddingRight="@dimen/activity_horizontal_margin"  
  8.    android:paddingTop="@dimen/activity_vertical_margin"  
  9.    tools:context=".MainActivity" >  
  10.    <com.google.android.maps.MapView  
  11.       xmlns:android="http://schemas.android.com/apk/res/android"  
  12.       android:id="@+id/mapView"  
  13.       android:layout_width="fill_parent"  
  14.       android:layout_height="fill_parent"  
  15.       android:clickable="true"  
  16.       android:apiKey="0UPSNoPt1QRgh5ctoV_t1vbo2lRHQbM2AsnEMeQ"  
  17.       />  
  18. </RelativeLayout>  
  19. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  20.    xmlns:tools="http://schemas.android.com/tools"  
  21.    android:layout_width="match_parent"  
  22.    android:layout_height="match_parent"  
  23.    android:paddingBottom="@dimen/activity_vertical_margin"  
  24.    android:paddingLeft="@dimen/activity_horizontal_margin"  
  25.    android:paddingRight="@dimen/activity_horizontal_margin"  
  26.    android:paddingTop="@dimen/activity_vertical_margin"  
  27.    tools:context=".MainActivity" >  
  28.    <com.google.android.maps.MapView  
  29.       xmlns:android="http://schemas.android.com/apk/res/android"  
  30.       android:id="@+id/mapView"  
  31.       android:layout_width="fill_parent"  
  32.       android:layout_height="fill_parent"  
  33.       android:clickable="true"  
  34.       android:apiKey="0UPSNoPt1QRgh5ctoV_t1vbo2lRHQbM2AsnEMeQ"  
  35.       />  
  36. </RelativeLayout> 
Step 3
 
Open the Java file as "MainActivity.java" and update it with the following code:
  1. package com.example.myfirstmap;  
  2. import android.os.Bundle;  
  3. import android.view.Menu;  
  4. import com.google.android.maps.MapActivity;  
  5. public class MainActivity extends MapActivity {  
  6.       @Override  
  7.       protected void onCreate(Bundle savedInstanceState) {  
  8.             super.onCreate(savedInstanceState);  
  9.              setContentView(R.layout.activity_main);  
  10.             /*WebView ww = (WebView)findViewById(R.id.mWebView); 
  11.             ww.loadUrl("http://www.google.com");*/  
  12.       }  
  13.       public boolean onCreateOptionsMenu(Menu menu) {  
  14.             // Inflate the menu; this adds items to the action bar if it is present.  
  15.             getMenuInflater().inflate(R.menu.main, menu);  
  16.             return true;  
  17.       }  
  18.       @Override  
  19.       protected boolean isRouteDisplayed() {  
  20.             // TODO Auto-generated method stub  
  21.             return false;  
  22.       }  

Step 4
 
Open the "AndroidManifest.xml" file and update it with the following code:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.myfirstmap"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.     <uses-sdk  
  7.         android:minSdkVersion="8"  
  8.         android:targetSdkVersion="17" />  
  9.         <uses-permission android:name="android.permission.INTERNET" />  
  10.     <application  
  11.         android:allowBackup="true"  
  12.         android:icon="@drawable/ic_launcher"  
  13.         android:label="@string/app_name"  
  14.         android:theme="@style/AppTheme" >  
  15.         <uses-library android:name="com.google.android.maps" />  
  16.         <activity  
  17.             android:name="com.example.myfirstmap.MainActivity"  
  18.                  android:theme="@android:style/Theme.NoTitleBar"  
  19.             android:label="@string/app_name" >  
  20.             <intent-filter>  
  21.                 <action android:name="android.intent.action.MAIN" />  
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26. </manifest> 
Step 6
 
The output is as shown in the following image:
 
map.jpg


Similar Articles