7
votes

I am using MapBox SDK for offline map I have added multiple markers on map, how to get click event for markers. Is there any way to get click event of marker?

5
how u resolved your issue @BSavaliyaErum
i have used ItemizedIconOverlay for marker clickBSavaliya
i m trying to use only one location that is user' current location on device with markerErum
This issue still seems to be quite popular, so it's worth clarifying that this issue is resolved in more recent versions of the Mapbox Maps SDk and @vanshg's answer is the best way to implement markers that can be clicked multiple times.riastrad

5 Answers

7
votes

I got the solution for marker click event in mapbox using a functionality provided by mapbox sdk called ItemizedIconOverlay.

I have done like following :

   public void placeGTMarker() {
        alMarkerGT = new ArrayList<Marker>();
        marker = new Marker("my Marker", "", latLng);
        marker.setMarker(activity.getResources()
                .getDrawable(R.drawable.map_pin));
        mv.addMarker(marker);
        alMarkerGT.add(marker);
        itemizedIconOverlayGT = new ItemizedIconOverlay(activity, alMarkerGT,
                new OnItemGestureListener<Marker>() {

                    @Override
                    public boolean onItemSingleTapUp(int index, Marker item) {
                        return false;
                    }

                    @Override
                    public boolean onItemLongPress(int index, Marker item) {
                        return false;
                    }
                });
        mv.addItemizedOverlay(itemizedIconOverlayGT);
    }

We can perform any event on onItemSingleTapUp for single click and for long click we can use onItemLongPress method.

I have used in my application and it works great

4
votes

You can set a MarkerClickListener on the MapboxMap

map.setOnMarkerClickListener(this);

and then have your class/activity/fragment implement MapboxMap.OnMarkerClickListener

@Override
public boolean onMarkerClick(@NonNull Marker marker) {
    return true;
}
0
votes
 @Override
public void onMapClick(@NonNull LatLng point) {

    if (destinationMarker != null) {
        mapboxMap.removeMarker(destinationMarker);
    }
    destinationCoord = point;
    destinationMarker = mapboxMap.addMarker(new MarkerOptions().position(destinationCoord));


    Geocoder coder = new Geocoder(RouteFinderNewC.this);
    List<Address> address;

    try {
        address = coder.getFromLocationName("Lahore pakistan", 1);
        if (address == null) {

        }
        assert address != null;
        Address location = address.get(0);
        double lat = location.getLatitude();
        double lng = location.getLongitude();
        destinationPosition = Point.fromLngLat(lat, lng);
        System.out.println("latitude and longitiude oof lahore ///////////" + lat + "  " + lng);
        originPosition = Point.fromLngLat(originCoord.getLongitude(), originCoord.getLatitude());
        getRoute(originPosition, destinationPosition);


    } catch (Exception e) {

    }




    Toast.makeText(this, "Kindly wait for finding suitable route for your.....", Toast.LENGTH_LONG).show();
    button.setEnabled(true);
    button.setBackgroundResource(R.color.mapboxBlue);

}
0
votes

Kotlin

    setContentView(R.layout.activity_main)
    mapView1 = findViewById(R.id.mapView)
    mapView1?.onCreate(savedInstanceState)
    mapView?.getMapAsync { mapboxMap ->
    mapboxMap.setOnMarkerClickListener(object: MapboxMap.OnMarkerClickListener {
            override
            fun onMarkerClick(@NonNull marker:Marker):Boolean {
                Toast.makeText(getApplicationContext(), marker.getTitle(), Toast.LENGTH_LONG).show()
                return true
            }
            })}
0
votes

MapBox v10 (Kotlin)

val annotationApi = mapView?.annotations
val pointAnnotationManager = annotationApi?.createPointAnnotationManager(mapView!!)
pointAnnotationManager?.addClickListener(object : OnPointAnnotationClickListener {
    override fun onAnnotationClick(annotation: PointAnnotation): Boolean {
        Toast.makeText(this@MainActivity, "Marker clicked", Toast.LENGTH_SHORT).show()
        return true
    }
})