How to get click event of marker using MapBox SDK?
Asked Answered
S

5

7

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?

Stirk answered 11/2, 2015 at 13:10 Comment(4)
how u resolved your issue @StirkUncommon
i have used ItemizedIconOverlay for marker clickStirk
i m trying to use only one location that is user' current location on device with markerUncommon
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
S
6

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

Stirk answered 18/5, 2015 at 6:6 Comment(3)
have u draw circle with zoom in animation in current location and on click it should zoom out that circleUncommon
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 overcomeUncommon
i m failed to get markers click using above method ?Uncommon
R
4

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;
}
Rebbeccarebe answered 18/8, 2016 at 21:42 Comment(2)
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/3176Rebbeccarebe
C
4

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
    }
})
Cahn answered 9/9, 2021 at 6:39 Comment(0)
T
2

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
            }
            })}
Thackeray answered 28/8, 2019 at 6:29 Comment(0)
T
0
 @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);

}
Torietorii answered 18/1, 2019 at 18:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.