1

I'm aiming to customize my markers (leaflet maps) into 3 different colours based on different values in an attribute column. Each marker is assigned a value A-C and I would like to represent each value as a different colour marker. I have been trying to use a switch statement but cannot seem to get it to work, any tips?

here is my current code:

    var customMarker = "A";

    switch(Data[i].value){
        case "A":
            markercolor (orange);
        break;
        case "B":
            markercolor (blue);
        break;
        case "C":
            markercolor (green);
        break;
        default :
            markercolor (pink);
        }

     var markerOptions = {
          color: customMarker,
          opacity: 1,
      };

     var marker = new L.circleMarker(markerLocation,markerOptions).addTo(map);
1
  • It's hard to guess from your code what are you trying to achieve with it. From your code it looks like you are try to assign color "A" to marker which makes no sense. What is function markercolor doing? What are values of orange, blue etc. variables?
    – TomazicM
    Commented May 12, 2019 at 13:50

1 Answer 1

1

I'd recommend something like the following:

    var colors = {
        orange: '#ffa500',
        blue: '#0000ff',
        green: '#008000',
        pink: '#ffc0cb',
    };

    var markerOptions = {
        opacity: 1,
    };

    switch(Data[i].value) {
        case "A":
            markerOptions.color = colors.orange;
            break;
        case "B":
            markerOptions.color = colors.blue;
            break;
        case "C":
            markerOptions.color = colors.green;
            break;
        default:
            markerOptions.color = colors.pink;
            break;
     }

     var marker = new L.circleMarker(markerLocation,markerOptions).addTo(map);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.