I have a Leaflet map app in which I am having difficulty getting the L.Control to function properly. The app pulls points based on data from a geoJson file. The geoJson data has a property "Type" (feature.properties.Type). The types are restaurants, bars, music, cafe, and culture. I want a Layer control legend that will toggle the points on and off the map depending on whether the use has them clicked in a checkbox. For example, if the user has bars clicked, but not culture, all the points with Type == 'bar' will show on the map but not those with 'culture'. This is how the geoJson is set up; mind that I already have variable to control what the icons look like that I am not including:

var points = L.geoJson(dtr_points, {
onEachFeature: function (feature, layer) {
    var link_url = "<a href='" + feature.properties.Link + "' target='_blank'>" + feature.properties.Name + "</a>"

     if(feature.properties.Type == "beer") {
     if(feature.properties.Type == "bar") {
     if(feature.properties.Type == "restaurant") {
     if(feature.properties.Type == "music") {
     if(feature.properties.Type == "culture") {
     if(feature.properties.Type == "cafe") {
     if(feature.properties.Type == "occult shop") {



Notice that the last line is where I am trying to add the Layer Control. It does not show up on the map currently. Can you suggest how to fix this? I think I need to add a layer group, but the Leaflet documentation doesn't explain how to do this for geoJson data.


1 Answer 1


Here's one approach

// first list the types in an array
var types = ['beer','bar','restaurant','music','culture','cafe']; // etc.
// add an empty layer control to the map
var layerControl = L.control.layers().addTo(map);

// iterate over types, filter by that type, and format the layer for that feature type
types.forEach(function(type) {
    var layer = L.geoJson(dtr_points, {
        filter: function(feature, layer) {
            return feature.properties.Type == type;
        onEachFeature: function(feature, layer) {
            var link_url = "<a href='" + feature.properties.Link + "' target='_blank'>" + feature.properties.Name + "</a>"
            // I don't see any L.icons in your example, but following what you have:
    // all done with the layer, add it to the control
    layerControl.addOverlay(layer, type);


Documentation for L.control.layers: http://leafletjs.com/reference.html#control-layers

  • That worked great! I see now that to utilize geoJson properties in the layer control, you must iterate through and format the properties and use addOverlay. I had to make a few modifications to accomodate my custom icons, but it is functioning nicely. Thanks for the insight!
    – gwydion93
    Mar 28, 2017 at 17:45
  • thanks a lot.. I learned a lot from this and was able to answer my own question with it: stackoverflow.com/q/72002480/6356278
    – Wimpel
    Apr 26, 2022 at 14:19

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.