Search code examples
androidonclicklisteneropenstreetmaposmdroid

How to expand setOnMarkerClickListener of OSMbonuspacket Marker without overriding default behavior?


I am working on an app that makes use of the OSMbonuspacket. I added markers with descriptions to the map. When clicking one of the markers, the description box is shown.

Now I want to call another function when tapping on a marker. Let's say I want to show a Toast. So I added the following setOnMarkerClickListener() function:

marker.setOnMarkerClickListener(new Marker.OnMarkerClickListener() {
    @Override
    public boolean onMarkerClick(Marker marker, MapView mapView) {
        Toast.makeText(context, "Hallo", Toast.LENGTH_LONG).show();
        return false;
    }
});

This shows the Toast when clicking the marker. However, the description box is not displayed anymore. I guess that I am overriding the default behavior. with this function. I made an app with a Google Maps integration once and did the same thing without any problems.

Does anyone know how to accomplish this with the OSMbonuspacket?


Solution

  • Default click listener for Marker in osmdroid look like this:

    protected boolean onMarkerClickDefault(Marker marker, MapView mapView) {
        marker.showInfoWindow();
        if(marker.mPanToView) {
            mapView.getController().animateTo(marker.getPosition());
        }
    
        return true;
    }
    

    so, you can override your listener like this:

    marker.setOnMarkerClickListener(new Marker.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker, MapView mapView) {
            marker.showInfoWindow();
            mapView.getController().animateTo(marker.getPosition());
            Toast.makeText(context, "Hallo", Toast.LENGTH_LONG).show();
            return true;
        }
    });