Search code examples
androidandroid-mapviewitemizedoverlay

ItemizedOverlay OnTap() not firing


I've defined a map overlay, and I can display markers without issue. I'm now trying to get something to happen when I tap one, but the event never seems to fire. I'm sure I'm missing something obvious...

public class MapBlobCollection extends ItemizedOverlay<OverlayItem> {

        @SuppressWarnings("serial")
        public class ItemTappedEvent extends EventObject
        {
            public ItemTappedEvent(int itemIndex) {
                super(itemIndex);
            }
        }

        private ArrayList<OverlayItem> myOverlays ;

        public MapBlobCollection(Drawable defaultMarker) {
            super(boundCenterBottom(defaultMarker));
            myOverlays = new ArrayList<OverlayItem>();
            populate();
        }

        public void addOverlay(OverlayItem overlay){
            myOverlays.add(overlay);
            populate();
        }

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

        // Removes overlay item i
        public void removeItem(int i){
            myOverlays.remove(i);
            populate();
        }

        // Returns present number of items in list
        @Override
        public int size() {
            return myOverlays.size();
        }


        public void addOverlayItem(OverlayItem overlayItem) {
            myOverlays.add(overlayItem);
            populate();
        }


        public void addOverlayItem(int lat, int lon, String title) {
            try {
                GeoPoint point = new GeoPoint(lat, lon);
                OverlayItem overlayItem = new OverlayItem(point, title, null);
                addOverlayItem(overlayItem);    
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }

        @Override
        protected boolean onTap(int index) {
            super.onTap(index);
            Log.d("TESTING","Triggering tap event on " + Integer.toString(index));
            EventManager.triggerEvent(this, new ItemTappedEvent(index));
            return true;
        }
}

Basically, the debug log entry isn't written and the event doesn't fire.

In addition, my mapview itself doesn't pan around (should it, without any extra code from me?) and despite setting the setBuitInZoomControls(true), these don't appear either... so perhaps the mapview itself is at fault?

The mapview is defined in the layout as:

<com.google.android.maps.MapView
android:id="@+id/indexMapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:apiKey="@string/mapskey_release"/>

And I'm not overriding any draw events or anything...


Solution

  • I believe you need to add

    android:clickable="true"
    

    to your mapview