Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get click event of marker using MapBox SDK?

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?

like image 806
BSavaliya Avatar asked Feb 11 '15 13:02

BSavaliya


4 Answers

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

like image 200
BSavaliya Avatar answered Nov 19 '22 00:11

BSavaliya


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;
}
like image 42
vanshg Avatar answered Nov 19 '22 00:11

vanshg


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
    }
})
like image 3
LIFED Avatar answered Nov 19 '22 01:11

LIFED


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
            }
            })}
like image 2
Juan de Dios Guadalupe Avatar answered Nov 19 '22 01:11

Juan de Dios Guadalupe