10

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 ?

fdelaneau
  • 125
  • 1
  • 7

5 Answers5

8

Screw jQuery

setMarkerColor(marker, color) {
      let markerElement = marker.getElement();
      markerElement
        .querySelectorAll('svg g[fill="' + marker._color + '"]')[0]
        .setAttribute("fill", color);
      marker._color = color;
    },
Max
  • 834
  • 8
  • 19
6

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

pathmapper
  • 1,777
  • 1
  • 11
  • 19
4

And if you need to change the color of the default marker on the fly you may use this kind of function (used jQuery to get element's children):

function setMarkerColor(marker, color) {
    var $elem = jQuery(marker.getElement());
    $elem.find('svg g[fill="' + marker._color + '"]').attr('fill', color);
    marker._color = color;
}
Anton Suslov
  • 103
  • 5
1

when setting up the marker, put the color in the brackets:

new mapboxgl.Marker({ "color": "#b40219" }) //this changes color to a dark red// .setLngLat([-74.5, 40]) .addTo(map);

0
marker = new mapboxgl.Marker({
        color: $parameters.UpdatedMarkerColor,
        draggable: false
    });
  • Please consider adding a brief explanation of [how and why this solves the problem](https://meta.stackoverflow.com/q/392712/13138364). This will help readers to better understand your solution. – tdy Nov 16 '21 at 04:22