Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapbox GL JS Change the color from the default marker

I’m starting to loose my mind.

According to Mapbox API I should be able to change the default marker color but I didn't find any exemple in the documentation that doesn't use custom markers and the most likely syntax doesn't work.

I am using mapbox-gl-js/v0.44.2

var marker = new mapboxgl.Marker({ "color": "#b40219" })
                .setLngLat([0, 0])
                .addTo(map);

With this code, the map is shown without the marker and if I remove the color option the marker does display correctly but with the wrong color.

Any suggestion to where I messed up ?

like image 770
fdelaneau Avatar asked Jun 17 '18 18:06

fdelaneau


2 Answers

Screw jQuery

setMarkerColor(marker, color) {
      let markerElement = marker.getElement();
      markerElement
        .querySelectorAll('svg g[fill="' + marker._color + '"]')[0]
        .setAttribute("fill", color);
      marker._color = color;
    },
like image 191
Max Avatar answered Sep 18 '22 17:09

Max


Support for custom colors when using the default Marker SVG element is available since v0.45.0, you are using v0.44.2.

Release notes:

https://github.com/mapbox/mapbox-gl-js/releases

like image 27
pathmapper Avatar answered Sep 17 '22 17:09

pathmapper