Thursday, May 17, 2012

Implement OnItemGestureListener on OpenStreetMap to detect user touch and retrieve touched items properties

Modify the code in last exercise "Create multi-marker OpenStreetMap for Android", add OnItemGestureListener for our ItemizedIconOverlay. Such that the OpenStreetMapView can react user touch. And also it can retrieve the touched item from the passed parameters. Then pass OnItemGestureListener to ItemizedIconOverlay in constructor.

Implement OnItemGestureListener on OpenStreetMap to detect user touch and retrieve touched items properties

package com.exercise.OpenStreetMapView;

import java.util.ArrayList;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.ItemizedIconOverlay;
import org.osmdroid.views.overlay.ItemizedIconOverlay.OnItemGestureListener;
import org.osmdroid.views.overlay.OverlayItem;
import org.osmdroid.views.overlay.ScaleBarOverlay;

import android.os.Bundle;
import android.widget.Toast;

public class AndroidOpenStreetMapViewActivity extends Activity {
 private MapView myOpenMapView;
 private MapController myMapController;
 ArrayList<OverlayItem> anotherOverlayItemArray;
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        myOpenMapView = (MapView)findViewById(;
        myMapController = myOpenMapView.getController();
        //--- Create Another Overlay for multi marker
        anotherOverlayItemArray = new ArrayList<OverlayItem>();
        anotherOverlayItemArray.add(new OverlayItem(
          "0, 0", "0, 0", new GeoPoint(0, 0)));
        anotherOverlayItemArray.add(new OverlayItem(
          "US", "US", new GeoPoint(38.883333, -77.016667)));
        anotherOverlayItemArray.add(new OverlayItem(
          "China", "China", new GeoPoint(39.916667, 116.383333)));
        anotherOverlayItemArray.add(new OverlayItem(
          "United Kingdom", "United Kingdom", new GeoPoint(51.5, -0.116667)));
        anotherOverlayItemArray.add(new OverlayItem(
          "Germany", "Germany", new GeoPoint(52.516667, 13.383333)));
        anotherOverlayItemArray.add(new OverlayItem(
          "Korea", "Korea", new GeoPoint(38.316667, 127.233333)));
        anotherOverlayItemArray.add(new OverlayItem(
          "India", "India", new GeoPoint(28.613333, 77.208333)));
        anotherOverlayItemArray.add(new OverlayItem(
          "Russia", "Russia", new GeoPoint(55.75, 37.616667)));
        anotherOverlayItemArray.add(new OverlayItem(
          "France", "France", new GeoPoint(48.856667, 2.350833)));
        anotherOverlayItemArray.add(new OverlayItem(
          "Canada", "Canada", new GeoPoint(45.4, -75.666667)));
        ItemizedIconOverlay<OverlayItem> anotherItemizedIconOverlay 
         = new ItemizedIconOverlay<OverlayItem>(
           this, anotherOverlayItemArray, myOnItemGestureListener);
        //Add Scale Bar
        ScaleBarOverlay myScaleBarOverlay = new ScaleBarOverlay(this);
    OnItemGestureListener<OverlayItem> myOnItemGestureListener
    = new OnItemGestureListener<OverlayItem>(){

  public boolean onItemLongPress(int arg0, OverlayItem arg1) {
   // TODO Auto-generated method stub
   return false;

  public boolean onItemSingleTapUp(int index, OverlayItem item) {
     item.mDescription + "\n"
     + item.mTitle + "\n"
     + item.mGeoPoint.getLatitudeE6() + " : " + item.mGeoPoint.getLongitudeE6(), 
   return true;

Download the files.

- Add MyLocationOverlay on OpenStreetMap, to display user location and compass.


Unknown said...

Hey friend, thanks, all tutorials are amazing. It is the only place where I found information on OpenStreetMap. All your work is amazing, I hope someday finish all your tutorials. :)
Sorry for my bad English
Greetings from Colombia,

Anonymous said...

Hey, it says it cannot resolve symbol item..
Do you know what to do?


Anonymous said...

It says it cannot resolve mDescription, mTitle and mGeoPoint..
where have you implemented those?