Ilias Tsagklis

About Ilias Tsagklis

Ilias Tsagklis is a senior software engineer working in the telecom domain. He is an applications developer in a wide variety of applications/services. Ilias 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;

    @Override
    public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

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

  mapView.setBuiltInZoomControls(true);

  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!");

  itemizedOverlay.addOverlay(overlayitem);

  mapOverlays.add(itemizedOverlay);

  MapController mapController = mapView.getController();

  mapController.animateTo(point);

  mapController.setZoom(6);

    }

    @Override
    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) {

  super(boundCenterBottom(defaultMarker));
   }

   public CustomItemizedOverlay(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) {

OverlayItem item = mapOverlays.get(index);

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

dialog.setTitle(item.getTitle());

dialog.setMessage(item.getSnippet());

dialog.show();

return true;
   }

   public void addOverlay(OverlayItem overlay) {

mapOverlays.add(overlay);

 this.populate();
   }

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

<RelativeLayout 
 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
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/map_view"
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent"
  android:clickable="true" 
  android:enabled="true" 
  android:apiKey="API-KEY-HERE" />

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

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

package="com.javacodegeeks.android.googlemaps"

android:versionCode="1"

android:versionName="1.0">

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

  <activity android:name=".GMapsActivity"

android:label="@string/app_name">

<intent-filter>

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

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

</intent-filter>

  </activity>

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

    </application>

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

</manifest>

 
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 two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

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

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close