0
votes

I am using Mapbox GL JS to display a lot of pins on a map with custom marker image:

map.on('load', function () {
  map.loadImage("/path/to/pin1.png", function(error, image) {
      if (error) throw error;
      map.addImage("custom-marker", image);
  });

  map.addLayer({
      id: "unclustered-point",
      source: "geolocations",
      filter: ["!", ["has", "point_count"]],
      type: "symbol",
      layout: {
          "icon-image": "custom-marker",
          "icon-allow-overlap": true,
          "icon-anchor": "bottom",
          "icon-size": 0.5
      }
  });

  //...

});

Is there any way to change a single marker image programmatically (e.g. the clicked marker)?

2
Lookup setFeatureState in the API docs, and feature-state in the style spec. - AndrewHarvey
Already tried that, it throws an error: "feature-state" data expressions are not supported with layout properties. - nagyadrian
Oh yes you're right sorry. In that case see my answer below. - AndrewHarvey

2 Answers

0
votes

You can add two layers, a "clicked" layer and an "unclicked" layer, then set the filter (setFilter) such that each layer filters only the clicked or unclicked features.

0
votes

I faced the same issue and finally, I found a way and documented my findings in my medium. This will help you.https://medium.com/@sameeraviraj/how-to-add-multiple-draw-points-in-mapbox-gl-js-a40235003650