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?
How to get click event of marker using MapBox SDK?
Asked Answered
how u resolved your issue @Stirk –
Uncommon
i have used ItemizedIconOverlay for marker click –
Stirk
i m trying to use only one location that is user' current location on device with marker –
Uncommon
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. –
Ephrayim
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
have u draw circle with zoom in animation in current location and on click it should zoom out that circle –
Uncommon
i only want to show marker image on current location of user so usage of UserLocationOverlay instad of alMarkerGT in ItemizedIconOverlay is not allowed ? how to overcome –
Uncommon
i m failed to get markers click using above method ? –
Uncommon
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;
}
In this approach you can't click same Marker twice one after another, it gets the first click but does not get other clicks on same Marker unless you click Map and then clic same Marker again. –
Hydroxide
Yeah, MapBox knows about this and I'm assuming they're working on it github.com/mapbox/mapbox-gl-native/issues/6099 github.com/mapbox/mapbox-gl-native/issues/3176 –
Rebbeccarebe
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
}
})}
@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);
}
© 2022 - 2024 — McMap. All rights reserved.