This article is outdated as Google Maps V1 is deprecated officially and no more supporting.
Consider reading updated article on Googe Maps Android Working with Google Maps V2

Google is providing an easy way to integrate Google Maps into your android applications. Today i am giving tutorial about embedding google maps in your android apps.

Download Code

Creating new project by selecting Google API SDK

While creating your project you need to select target SDK as Google API. If you don’t have one already download Google API SDK by opening your SDK Manager

1. Create a new project by going to File ⇒ New Android Project. Fill all the details, select Google API as target sdk and name your activity.

Android Google API SDK

2. As Google Map libraries are not part of Android libraries we need to mention library in AndroidManifest.xml file. Also we need to give permission to connect to internet. Open your AndroidManifest.xml file and modify as below.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidhive.googlemaps"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        
        <!--  Add Google Map Library -->
        <uses-library android:name="com.google.android.maps" />
        
        <activity
            android:label="@string/app_name"
            android:name=".AndroidGoogleMapsActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    
    <!-- Allow to connect with internet -->
    <uses-permission android:name="android.permission.INTERNET" />

</manifest>

Obtaining Google Map Key

In order to use google maps you need to get map key. I am assuming you are working on windows PC. Now you need to get MD5 key from your jdk installation.

3. Open your command prompt by typing cmd in your run. Start ⇒ Run ⇒ type cmd.

c:\<path-to-jdk-dir>\bin\keytool.exe -list -alias androiddebugkey -keystore "C:\users\<user-name>\.android\debug.keystore" -storepass android -keypass android

below is how got my MD5 fingerprint

keytool.exe -list -alias androiddebugkey -keystore "C:\users\ravi\.android\debug.keystore" -storepass android -keypass android
android maps generating MD5

4. Now you need to get map key using MD5 fingerprint. Go to Sign Up for the Android Maps API and get your map key by giving MD5 fingerprint.

Android Google Map Key

Android Google Map Key

5. Now open your main.xml file under src ⇒ layout ⇒ main.xml and insert the following code. Don’t forgot to place your key.

<?xml version="1.0" encoding="utf-8"?>
<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="05M-7qOGbEjYduPPUdQgJt9ysL8HToawGdvu_ow"
/>

6. Now open your MainActivity class and extend it from MapActivity.

public class AndroidGoogleMapsActivity extends MapActivity { 

7. After extending your activity from MapActivity you need to override isRouteDisplayed() and also set your default view to main.xml

package com.androidhive.googlemaps;

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

public class AndroidGoogleMapsActivity extends MapActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
    }

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}

8. Now run your project, you should see map displaying on your device. If don’t please redo all the steps above.

android google maps

Displaying Zooming Controls

Until now we have a static map with drag and move features only. We can add zooming controls in order to zoom in or zoom out maps. Google maps had inbuilt zooming controls, so all we need to do is call couple of lines.

package com.androidhive.googlemaps;

import android.os.Bundle;

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

public class AndroidGoogleMapsActivity extends MapActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // Displaying Zooming controls
        MapView mapView = (MapView) findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);
        
        
    }

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}
Android google Maps

Changing Map Display Type

You can also change map type like satellite, streetview etc.,

mapView.setSatellite(true); // Satellite View
mapView.setStreetView(true); // Street View
mapView.setTraffic(true); // Traffic View
Android google map types

Showing Location by passing Latitude and Longitude

Below code will show a location on the map by passing latitude and longitude of that location.

MapController mc = mapView.getController();
double lat = Double.parseDouble("48.85827758964043"); // latitude
double lon = Double.parseDouble("2.294543981552124"); // longitude
GeoPoint geoPoint = new GeoPoint((int)(lat * 1E6), (int)(lon * 1E6));
mc.animateTo(geoPoint);
mc.setZoom(15);
mapView.invalidate(); 
Android google maps by latitude and longitude

Adding Marker on the map

For displaying marker on the map you need to create new class which extends ItemizedOverlay.
Create new class and name it as AddItemizedOverlay.java and write following code.

package com.androidhive.googlemaps;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {
	   
	   private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
	   
	   private Context context;
	   
	   public AddItemizedOverlay(Drawable defaultMarker) {
	        super(boundCenterBottom(defaultMarker));
	   }
	   
	   public AddItemizedOverlay(Drawable defaultMarker, Context context) {
	        this(defaultMarker);
	        this.context = context;
	   }

	   @Override
	   protected OverlayItem createItem(int i) {
	      return mapOverlays.get(i);
	   }

	   @Override
	   public int size() {
	      return mapOverlays.size();
	   }
	   
	   @Override
	   protected boolean onTap(int index) {
		  Log.e("Tap", "Tap Performed");
	      return true;
	   }
	   
	   public void addOverlay(OverlayItem overlay) {
	      mapOverlays.add(overlay);
	       this.populate();
	   }

	}

Now open your MainActivity.java (in my case AndroidGoogleMapsActivity.java) and write following code.

List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.mark_red);
AddItemizedOverlay itemizedOverlay = 
new AddItemizedOverlay(drawable, this);
        
        
OverlayItem overlayitem = new OverlayItem(geoPoint, "Hello", "Sample Overlay item");
        
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
Android maps placing marker

Getting Latitude and Longitude of location that was touched

You can also get the latitude and longitude of location which was touched. Open your AddItemizedOverlay.java and add following method.

package com.androidhive.googlemaps;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {

       /*................. Add this method ........*/
       @Override
       public boolean onTouchEvent(MotionEvent event, MapView mapView) 
       {   
           
           if (event.getAction() == 1) {                
               GeoPoint geopoint = mapView.getProjection().fromPixels(
                   (int) event.getX(),
                   (int) event.getY());
               // latitude
               double lat = geopoint.getLatitudeE6() / 1E6;
               // longitude
               double lon = geopoint.getLongitudeE6() / 1E6;
               Toast.makeText(context, "Lat: " + lat + ", Lon: "+lon, Toast.LENGTH_SHORT).show();
           }                            
           return false;
       } 

	}

Now run your project and touch a particular location to get lat and long of that location.

google maps getting latitude and longitude
This is image is for thumbnail purpose
Android Google maps
Ravi is hardcore Android programmer and Android programming has been his passion since he compiled his first hello-world program. Solving real problems of Android developers through tutorials has always been interesting part for him.
  • Pingback: Android Working with Google Maps | AndroidHive | Tutorials, Games … | pimproid.com()

  • Gentor_sic

    Great tutorial! DO you have a tutorial on getting the location from an edittext?

  • Boopathy Balakrishnan

    Hi Ravi how to use Google Map Android api v2?

  • Panchanan Rauta816

    it is really though full and good.

  • Krunal Dave1988

    hi running this program in emmulator just shows the grid pin and zoom in zoom out control .no maps are displayed.

    • Anukool Tiwari

      Generate the api key and add it in the main.xml android:apikey attribute.
      Hope this helps.

      • Nazrul

        Hi Mate, ive done this and still see the same as above post.

        Thanks,

      • Pavan Obbeli89

        i have put apikey it shows icon with latitude and longitude but didin’t show maps

  • Luiz

    Hi Ravi, could you please do a tutorial on how to obtain the new Google Maps Android API v2, and integrate it in an app. Thanks

  • Jindra

    Great tutorial.. but i dont see the map either :/ only the grid with the pin and the toolbar. Could you pls give me an advice? Thx a lot

  • Anoop

    When Generate fingerprint using cmd, i am getting a SH1 code instead of MD5. Google is not accepting that code to generate API. What to do?

  • Kamalakar Damera

    hey thanks man…..it helps me alot…but i didnt get the googlemap……. instead i got small boxes at the output…please reply me

  • mahesh gurram

    hi running this program in emmulator just shows the grid pin and zoom in zoom out control and icon ,when move icon then show a message but no maps are displayed.

  • Iram Bukhari

    Really …very informative post…thanks for sharing..:)

  • Barry Maswan

    Great post man! However, I keep on getting some error in my Styles.xml. This what is displayed in the console whenever I try to run the app
    [2013-01-15 12:17:57 – GoogleMaps] W/ResourceType( 4149): Bad XML block: header size 56 or total size 154188856 is larger than data size 0
    [2013-01-15 12:17:57 – GoogleMaps] /home/barry/workspace/GoogleMaps/res/menu/activity_android_google_maps.xml:3: error: No resource identifier found for attribute ‘showAsAction’ in package ‘android’

    Please help me

  • Gauravfree2rhyme

    if you use proxy on your network please enter
    -http-proxy http://10.1.8.30:8080
    in your additional input parameters for emulator

  • Nazrul

    Hi there, all I get is a blank canvas with gridlines. No actual map is displayed?

    Thanks,

    • jayant shinde

      Please use your own google maps api key not the one posted above.

  • Tcwijekoon

    Very useful post. Thank you

  • Reyajali

    awesome tutorial, it’s nice working and also very helpful for startups.

  • Ashutosh

    Nice post ravi please tell how to plot two llocation on map

  • Danish

    Hello Sir, I have tried it but only Grids on screen is viewing. i am not getting the original view of Map. Plz Help Me
    @DANISH

    • jayant shinde

      Hi Danish please use your own google maps api not the one posted above.

  • priyatham naga

    i am not getting google api in android sdk…wat to do?

  • Vishal Garg

    fadu bhai fadu!!!!!!!!!!!!!!
    Properly running

  • Merry Vino

    Check the manifest file. Add all the uses permission needed before run the application..Check Google Maps Android API v2 for this…..

  • Dhiraj

    hay i try this code on my eclipse but pin point is not displaying so could u tell me what necessary requirement need like- which sdk version or eclipse version???

  • Darshilrami

    In my application the map is shown but the marker does not shown….and when i used the code to show the latitude and longitude the application shows force close..please give some solution..

  • Brianmaldonado

    Hello from mexico! thank you very much for this work, I keep on all steps, but in step eight, didn’t drew the map, why?? I Can see only empty boxes in my aplication, well, I have been tried for three days and don’t can, I hope for you reply, take care.

  • Ubongmajesty

    Hello Ravi, i saw your Tutorial on Android application, this an amazing work. i really appreciate your Effort and time. please i have problem with my application. i can only see the grip but no actual map displaying .i have insert both my Map key and API key but still have the same problem. Please kindly help. thank you for your kind consideration. John

  • Karthickbe2008

    Such a nice tutorial you given to us,can you tell us how to do the search option to find the location using google map

  • Steven Perete

    is there an Maps API v2 example which uses MapFragment

  • Mrjadulco

    hi… nice tutorial… but i have a problem… when i run it in the emulator,, it works… but when i exported it and install the apk in the tablet… it didn’t work… may you please tell me why it happen and what should i do??? i hope you can help me… thanks very much… more power… ^^,

  • You are boss man!
    I love android hive

  • Giancarlo Leonio

    Thanks for the tutorial! I compiled a list of top resources for adding a MapView to an Android Application. I included your post. Check it out/ feel free to share. http://www.verious.com/board/Giancarlo-Leonio/adding-a-map-view-to-an-android-application/ Hope other developers find this useful too 🙂

  • Srikanthlraj Raj

    hey i want overlay on All Location..How can i Achived it??