Monday, July 30, 2012

Google Map Tutorial Android (Basic)


In this tutorial we will learn how can we add google map into our android app in a very easy & simple way.

Note:  Because the Maps library is not a part of the standard Android library, you must  remember four things at-least to get G-Map in our app .

1. Obtaining a Google Maps Android API Key to get G-Map view in our app.
2.Select Android SDK with Google APIs (when create new app)
3. We need to extends MapActivity
4. Your emulator also created with Google API SDK to run Google map apps (before run app).

so let's try this small app

First is first: Obtain Google Maps Android API
***************************************************************************
1. Create a folder in C drive called "C:\Android"

2. Go to this path "C:\Program Files\Java\<JDK_version_number>\bin"
or where you installed java, copy "keytool.exe" file and put in our created folder " Android ".

3. Now we need to look for "debug.keystore" for probably you can find here

  •     Windows Vista: C:\Users\\.android\debug.keystore
  •     Windows XP: C:\Documents and Settings\\.android\debug.keystore
  •     OS X and Linux: ~/.android/debug.keystore 

In my system i got here "C:\Users\RDC\.android\debug.keystore"

Now copy this file and put into our folder " Android "

4. open command prompt and go to bin folder in prompt.
 then hit command
--------------------------------------------------------------------------------------------------------
keytool.exe -list -alias androiddebugkey -keystore "C:\android\debug.keystore" -storepass android -keypass android
--------------------------------------------------------------------------------------------------------
Now we need to copy MD5 key so right click on command prompt and "mark" then copy and save in Text file. see below image...



Copy MD5 key and open this developer-page and sign in.
then paste here MD5 fingerprint and Generate API key this way


after generating key the result screen will be


you can use this key whenever wants to develop Google map app in this development environment only.

Okay Great!! now we are ready to go...make sure don't use mine key its valid only for my development environment so use your own key :D

*****************************************************************************


Let's create new android app this way..

 -------------------------------------------
App Name: AlertBoxBasic
Package Name: com.rdc
Android SDK: Android SDK 2.3.3 / API 10
Default MapActivity Name: ActivityGoogleMap
-------------------------------------------

ActivityGoogleMap.java
package com.rdc.gmap;

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

public class ActivityGoogleMap extends MapActivity {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //to add zoom in and zoom out default buttons
        MapView mapView = (MapView) findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);
    }
    
    @Override
 protected boolean isRouteDisplayed() {
  // TODO Auto-generated method stub
  return false;
 }
}

look important Google API key in main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <com.google.android.maps.MapView
  android:id="@+id/mapView"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:enabled="true"
  android:clickable="true"
  android:apiKey="0iuCzAK4N1AoTya_fr62sB7NXXPqkWqF-OCNMEg" />

</LinearLayout>


and Manifest file must be like
<?xml version="1.0" encoding="utf-8"?>
<manifest
 xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.rdc.gmap"
 android:versionCode="1"
 android:versionName="1.0">
 <uses-sdk android:minSdkVersion="10" />
 <uses-permission android:name="android.permission.INTERNET" />
 <application
  android:icon="@drawable/icon"
  android:label="@string/app_name">

  <uses-library android:name="com.google.android.maps" />
  <activity
   android:name=".ActivityGoogleMap"
   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>

Now create Emulator using Google API SDK see below image

The output Screen will be like this..


You can download the complete source code zip file here : GoogleMapBasic

 cheers!!

 I'd love to hear your thoughts!

2 comments:

  1. thankyou so much for sharing these codes but I import your project and tryd to implement it but i am facing errors around 39.

    ReplyDelete
  2. This was great!! Thank you so much!!

    ReplyDelete