12

I am trying to add a legend to my leaflet map and no matter what I try I cannot get it to display properly. I have imported a point level geojson into my map and I have styled the points in a similar way to how it's done within the Leaflet Choropleth example:

function getColor(d) {
        return d === 'Road Surface'  ? "#de2d26" :
               d === 'Signage'  ? "#377eb8" :
               d === 'Line Markings' ? "#4daf4a" :
               d === 'Roadside Hazards' ? "#984ea3" :
                            "#ff7f00";
    }

    function style(feature) {
        return {
            weight: 1.5,
            opacity: 1,
            fillOpacity: 1,
            radius: 6,
            fillColor: getColor(feature.properties.TypeOfIssue),
            color: "grey"

        };
    }

I can't add the code for the data because it contains sensitive information but I have added the geojson and have added some popups to it.

When it comes to the legend here is my code:

var legend = L.control({position: 'bottomleft'});
    legend.onAdd = function (map) {

    var div = L.DomUtil.create('div', 'info legend');
    labels = ['<strong>Categories</strong>'],
    categories = ['Road Surface','Signage','Line Markings','Roadside Hazards','Other'];

    for (var i = 0; i < categories.length; i++) {
            div.innerHTML += 
            labels.push(
                '<i style="background:' + getColor(categories[i] + 1) + '"></i> ' +
                (categories[i] ? categories[i] : '+'));
        }

        div.innerHTML = labels.join('<br>');
    return div;
};

legend.addTo(map);

This code brings up a box that contains a title and the categories that I have defined, but no colours, which is the thing that I can't work out.

1
  • I am having a similar issue, but my legend only shows one color for the different categories. But my layer coloring is correct.![enter image description here](i.stack.imgur.com/T79CS.jpg) By the way your code really helped me out! I have been struggling with categorical data for a while! The chloropleth was easy.
    – JohnGIS
    Feb 19, 2018 at 9:21

2 Answers 2

12

I found the problem. The code that I posted in the question above was correct but I had made a schoolboy error in the css part.
I am pretty new to leaflet and coding in general and there is very little out there on how to produce legends (other than the Choropleth example). Here is the code that works for me that produces a legend like this: enter image description here

var legend = L.control({position: 'bottomleft'});
    legend.onAdd = function (map) {

    var div = L.DomUtil.create('div', 'info legend');
    labels = ['<strong>Categories</strong>'],
    categories = ['Road Surface','Signage','Line Markings','Roadside Hazards','Other'];

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

            div.innerHTML += 
            labels.push(
                '<i class="circle" style="background:' + getColor(categories[i]) + '"></i> ' +
            (categories[i] ? categories[i] : '+'));

        }
        div.innerHTML = labels.join('<br>');
    return div;
    };
    legend.addTo(map);
4
  • 2
    You could have say what is the error instead of just putting some code. Hard to find what's different... Aug 9, 2021 at 14:37
  • Yeah this doesn't work for me, circles don't appear
    – pyCthon
    Oct 16, 2022 at 22:29
  • '<i class="bi bi-circle-fill" style="color:' + I replaced the above circle and style with this using bootstrap circles to get it to work for me
    – pyCthon
    Oct 16, 2022 at 22:41
  • How would you do this for multiple layers and have a different legend for each layer that disappeared and reappeared when its corresponding layer is checked in the layers dropdown menu?
    – Will
    Nov 21, 2022 at 20:58
0

You could check if your function"get color"is written inside some other function(for example some sort of init function and your for loop not.what happens if you run getColor(categories[0]) in your console? Apart from that categories[i] + 1 should be just categories[i] in order not to get problems during your last loop...

1
  • 1
    Thanks for getting back to me. 'Get Color' is also added when i'm calling the geojson as a circle marker: L.geoJson(RRData, { style: style, pointToLayer: function (feature, latlng) { var popupOptions = { maxWith: 300 };var popupContent = "<b>Location: </b>" + feature.properties.ActualLocation + "</br>" + "<b>Issue: </b>" + feature.properties.TypeOfIssue + "</br>" + "<b>Information: </b>" + feature.properties.Information; return L.circleMarker(latlng, getColor(feature.properties.typeofissue)).bindPopup(popupContent, popupOptions); } }).addTo(map); Could this be where the problem is? Feb 9, 2015 at 23:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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