Home » Android » core » google maps » Google Maps example

About Ilias Tsagklis

Ilias Tsagklis
Ilias is a senior software engineer working in the telecom domain. He is an applications developer in a wide variety of applications/services, currently the technical lead in a in-house PCRF solution. Particularly interested in multi-tier architecture, middleware services and mobile development (contact). Ilias Tsagklis is co-founder and Executive Editor at Java Code Geeks.

Google Maps example

With this example we are going to demonstrate how to use Google maps on Android. Note that in order to use Google Maps, the Google APIs have to be present in your SDK. In case they are not already installed, you will have to manually install them. This is accomplished by using the Android SDK and AVD Manager.

So, in order to use Google maps, the following steps will help you:

  • Create a class that extends the com.google.android.maps.MapActivity class (here called GMapsActivity)
  • Create a resource XML file that includes the com.google.android.maps.MapView
  • Use an instance of the com.google.android.maps.MapView class by taking reference of the one defined in the XML
  • Create a class that extends com.google.android.maps.ItemizedOverlay (here called CustomItemizedOverlay)
  • Implement the onTap of the ItemizedOverlay class
  • Launch the activity as usual via the XML launcher file

These are demonstrated in the code snippet(s) below:

package com.javacodegeeks.android.googlemaps;

import java.util.List;

import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class GMapsActivity extends MapActivity {

    private MapView mapView;

    private static final int latitudeE6 = 37985339;
    private static final int longitudeE6 = 23716735;

    public void onCreate(Bundle savedInstanceState) {



  mapView = (MapView) findViewById(R.id.map_view);


  List<overlay> mapOverlays = mapView.getOverlays();

  Drawable drawable = this.getResources().getDrawable(R.drawable.icon);

  CustomItemizedOverlay itemizedOverlay = 

 new CustomItemizedOverlay(drawable, this);

  GeoPoint point = new GeoPoint(latitudeE6, longitudeE6);

  OverlayItem overlayitem = 

 new OverlayItem(point, "Hello", "I'm in Athens, Greece!");



  MapController mapController = mapView.getController();




    protected boolean isRouteDisplayed() {

  return false;

package com.javacodegeeks.android.googlemaps;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;

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

public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> {

   private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();

   private Context context;

   public CustomItemizedOverlay(Drawable defaultMarker) {


   public CustomItemizedOverlay(Drawable defaultMarker, Context context) {


  this.context = context;

   protected OverlayItem createItem(int i) {

return mapOverlays.get(i);

   public int size() {

return mapOverlays.size();

   protected boolean onTap(int index) {

OverlayItem item = mapOverlays.get(index);

AlertDialog.Builder dialog = new AlertDialog.Builder(context);




return true;

   public void addOverlay(OverlayItem overlay) {



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


  android:apiKey="API-KEY-HERE" />

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

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




    <application android:icon="@drawable/icon" android:label="@string/app_name">

  <activity android:name=".GMapsActivity"



    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />



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


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


This was an Android Google Maps example.

Related Article:

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....


Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.