Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Handling Touch Events on an OpenStreetMap Overlay

In Chapter: Location and Map Applications
Author: Rachee Singh ('racheesingh')
Published? true
FormatLanguage: WikiFormat


You need to perform actions when the overlay on an OpenStreetMap is tapped


Overriding the methods of OnItemGestureListener method for single tap events and long press events.


To address touch events on the map overlay, we modify the way we instantiate an overlay item (To see more details about using overlays in OSM, check out: Creating overlays in OpenStreetMaps ). While instantiating the OverlayItem, we make use of an anonymous object of OnItemGestureListener class as an argument and provide our own implementation of onItemSingleTapUp and onItemLongPress methods. In these methods, we simply display a toast depicting which action took place: single tap or long press and also the title and description of the overlay touched.

Here is how the code looks:

        ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
        items.add(new OverlayItem("New Overlay", "Overlay Sample Description", overlayPoint));

        resourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
        this.myLocationOverlay = new ItemizedIconOverlay<OverlayItem>(items,
                new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
                    public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
                        Toast.makeText( getApplicationContext(), "Overlay Titled: " +
item.mTitle + " Single Tapped" + "\n" + "Description: " + item.mDescription, Toast.LENGTH_LONG).show();
                        return true; 
                    public boolean onItemLongPress(final int index, final OverlayItem item) {
                        Toast.makeText( getApplicationContext(), "Overlay Titled: " + 
item.mTitle + " Long pressed" + "\n" + "Description: " + item.mDescription ,Toast.LENGTH_LONG).show();
                        return false;
                }, resourceProxy);

On tapping the overlay once, this is how the application looks:

On long pressing the overlay, this is how the application looks:


The source code for this project can be downloaded from https://docs.google.com/leaf?id=0B_rESQKgad5LMzZmMjJkZjYtN2M1OC00MGEzLWI2ZTQtNTUxMzFhZjEzMGIx&hl=en_US .