Search code examples
androidmapbox-androidmapbox-marker

How to detect MarkerView click in native Android Mapbox map?


This Mapbox code is now deprecated:

mapboxMap.setOnMarkerClickListener(new MapboxMap.OnMarkerClickListener() {
    @Override
    public boolean onMarkerClick(@NonNull Marker marker) {

        // Show a toast with the title of the selected marker
        Utilities.makeToast(requireContext(), marker.getTitle());
        return true; // Prevents standard InfoWindow from being displayed
    }
});

The documentation says to use the Mapbox Annotation Plugin instead. I've read that and have also checked out the MarkerView example but no clues anywhere on how to detect when a marker (MarkerView) has been clicked.

Also, the MarkerViewManager and MarkerView classes don't seem to have any methods available for detecting marker clicks.

Any ideas?


Solution

  • When creating a MarkerView, you supply a custom view to the constructor, e.g.,:

    MarkerView markerView = new MarkerView(new LatLng(latitude, longitude), customView);
    mMarkerViewManager.addMarker(markerView);
    

    So, to get the marker click event, you first call:

    customView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Utilities.makeToast(requireContext(), "Marker clicked!");
        }
    });