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?
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
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;
}
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
}
})
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
}
})}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With