Search code examples
javaandroidopenstreetmaposmdroid

LongPress on OSMDroid map is not working


Im creating an OpenStreetMaps application using OSMDroid lib. I need LongPress gesture to add markers into an OverlayItem array to show them all, but i can't access to LongPress anyway.

I have tried with GestureListener, overridden the onTouch event, using GestureDetector(which is deprecated nowadays) and I can't find a solution for this problem.

The only way i have get the gesture is through setOnLongClickListener, but I have to choose between having longpress or panning the map, and I'm greedy and I want both :p

Have anyone any ideas?

Regards!


Solution

  • I have encounter a solution through OSMBonusPack library.

    The object MapsEventsReceiver grants you a simple way to handle map's gestures. You have to override singleTapUpHelper and longPressHelper methods and create a new overlay with this handler.

    Here's the code:

    MapEventsReceiver mReceive = new MapEventsReceiver() {
    
        @Override
        public boolean singleTapUpHelper(IGeoPoint arg0) {
         Log.d("debug", "Single tap helper");
         //your onSingleTap logic here
        return false;
        }
    
        @Override
        public boolean longPressHelper(IGeoPoint arg0) {
        Log.d("debug", "LongPressHelper");
        //your onLongPress logic here
        setMarker(arg0);
        return false;
        }
    };
    
    //Creating a handle overlay to capture the gestures
    MapEventsOverlay OverlayEventos = new MapEventsOverlay(getBaseContext(), mReceive);
    map.getOverlays().add(OverlayEventos);
    
    //Refreshing the map to draw the new overlay
    map.invalidate();
    
    
    //I use this method to set the marker on touchPoint
        protected void setMarker(IGeoPoint arg0) {
    
    touchPoint = (GeoPoint) arg0;
    targetPoint = touchPoint;
    path.addPoint(targetPoint);
    
    //Listener to handle item's(markers) events
     myOnItemGestureListener = new OnItemGestureListener<OverlayItem>() {
    
        @Override
        public boolean onItemLongPress(int arg0, OverlayItem arg1) {
        Log.d("debug", "Testing long tap on item");
        //your item onLongPress logic here
            return false;
        }
    
        @Override
        public boolean onItemSingleTapUp(int index, OverlayItem item) {
        Log.d("debug", "Testing single tap on item");
        //your item onSingleTap logic here
            return true;
        }
    };
    
    itemarray.add(new OverlayItem("Marker " + cnt, "Info about marker " + cnt, touchPoint));
    
    ItemizedOverlayWithFocus<OverlayItem> overlayDeItems = new ItemizedOverlayWithFocus<OverlayItem>(getBaseContext(), itemarray, myOnItemGestureListener);
    map.getOverlays().add(overlayDeItems);
    
    overlayDeItems.setFocusItemsOnTap(true);
    map.invalidate();
    itemarray = new ArrayList<OverlayItem>();
       cnt++;
    }