5

I'm aiming to have my markers in three different colours depending on their rating property. I have seen a similar post where an object was used to define the colours. Each marker has a rating attribute between 1 and 5.

I was thinking of using a else if statement e.g

if (rating < 3) {
    markerColor: 'red'
} else if (rating = 3 ) {
    markerColor: 'orange'
} else {
    markerColor: 'green'
}

I am creating my markers as follows:

for (var i = 0; i < data.length; i++)   {

    var customOptions = {
        'maxWidth': '500',
        'className' : 'custom'
    };

    //Custom icon
    var blueMarker = L.AwesomeMarkers.icon({
        markerColor: 'blue'
    });

    //Create markerLocation variable    
    var markerLocation = new L.LatLng(data[i].lat, data[i].lon);

    //Create marker variable
    var marker = new L.Marker(markerLocation, {icon: blueMarker});

    marker.bindPopup("<p><h2>Rating:</h2> " + data[i].rating_value,
                     customOptions);
}

Would the else if statement be used when assigning the blueMarker variable?

Thanks

I used

  var = customColour = "green";

  if (data[i].rating_value < 3)
    customColor = "red";
  else if (data[i].rating_value === 3)
    customColor = "orange";

//Create custom icon
  var customMarker = L.AwesomeMarkers.icon({
  markerColor: customColour
  });

//Create markerLocation variable    
  var markerLocation = new L.LatLng(data[i].lat, data[i].lon);

//Create marker variable
  var marker = new L.Marker(markerLocation, {icon: customMarker});

However the AwesomeMarkers plugin only accepts colours, so I dont think using customColour worked. https://github.com/lvoogdt/Leaflet.awesome-markers. Thanks

1
  • Is it a typo customColor vs customColour?
    – xmojmr
    Commented Apr 26, 2017 at 5:34

2 Answers 2

4

You'd use the conditional statement to determine the value which gets passed into the markerColor property. In my example below, I store the determined colour within a variable named customColor:

var customColor = "green";

if (rating < 3)
  customColor = "red";
else if (rating === 3)
  customColor = "orange";

var blueMarker = L.AwesomeMarkers.icon({
  markerColor: customColor
});
2
  • Thanks, rating is actually in an array at data[i].rating_value, do i need to create a variable equal to this and use that instead?
    – Steven
    Commented Apr 25, 2017 at 16:48
  • If you're referring to it twice you should store it in a variable, otherwise you can replace rating in my example above with that. Commented Apr 25, 2017 at 16:49
1

I would use a function so you can easily add more keys in the future. It would look something like:

function determineColor(rating) {
  if ( rating < 3) {
    return 'red';
  } else if ( rating === 3 ) {
    return 'orange';
  } else {
    return 'blue';
  }
}

var Marker = L.AwesomeMarkers.icon({
  markerColor: determineColor(rating)
});

Also reminder one = assigns values to variables and === checks for equality.

4
  • So assign var rating = data[i].rating_value at the top of the the code?
    – Steven
    Commented Apr 25, 2017 at 17:21
  • Thanks. The method works, although all the markers are blue, even if their rating is 3 or less. I don't think the variable assignment is correct.
    – Steven
    Commented Apr 25, 2017 at 17:41
  • Try moving the rating assignment into the body of the for loop @Steven
    – maxwell
    Commented Apr 25, 2017 at 18:06
  • Cool! If you liked the answer / it worked for you please consider upvoting / marking correct, thanks! @Steven
    – maxwell
    Commented Apr 25, 2017 at 18:15

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