How to detect MarkerView click in native Android Mapbox map?

752 Views Asked by At

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?

1

There are 1 best solutions below

0
On BEST ANSWER

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!");
    }
});