Using custom icons for Leaflet Layer groups?How to use custom icons for Leaflet Layer groups with geojson...

Disk space full during insert, what happens?

How many diagrams is too much in a research article?

Do the speed limit reductions due to pollution also apply to electric cars in France?

In the Lost in Space intro why was Dr. Smith actor listed as a special guest star?

How do I add a strong "onion flavor" to the biryani (in restaurant style)?

Can you help me solve this algebra problem?

What does an unprocessed RAW file look like?

Third wheel character

What does "south of due west" mean?

How can I prevent an oracle who can see into the past from knowing everything that has happened?

What is an explicit bijection in combinatorics?

Boss asked me to sign a resignation paper without a date on it along with my new contract

Protagonist constantly has to have long words explained to her. Will this get tedious?

What is wrong with my use of "find -print0"?

How long can the stop in a stop-and-go be?

How can I give a Ranger advantage on a check due to Favored Enemy without spoiling the story for the player?

Select all columns except geometry using virtual layers

User input happy birthday program

Expression for "unconsciously using words (or accents) used by a person you often talk with or listen to"?

Can you prevent a man in the middle from reading the message?

Why do single electrical receptacles exist?

Taking an academic pseudonym?

Can you say "leftside right"?

Why did Ylvis use "go" instead of "say" in phrases like "Dog goes 'woof'"?



Using custom icons for Leaflet Layer groups?


How to use custom icons for Leaflet Layer groups with geojson loaded XHRHow to plot multiple markers on a leaflet map?How to get LatLng array from geoJSON Layer in Leaflet?Shared markers/paths in layer groups - toggle issuesHow to add layers and update layer control dynamically : leafletresize divIcons (svgs) at zoom levels - LeafletHow to update position of markers with “real time gps coordinants”?MarkerCluster don't work with geojson layer in leafletEsri leaflet layers turned off by default in map layer control boxWhy can't I load my basemap from the arcgis rest services using leaflet?Leaflet create marker from ajax call data













2















I have searched posts and tutorials and could not find the answer to my specific application of this feature



Almost all the examples use L.marker to simply place a marker on the map using various custom icons. This would be fine except I am not just placing markers but rather adding all the geoJson data to layers, then adding those layergroups to the control and then to the map. - I.E. I want all the layer routeca1 to use a red icon. I think I have narrowed this to needing to use the point to layer function:



pointToLayer: function (feature, latlng) {
return L.marker(latlng, {icon: redIcon});
}


I am not using the onEachFeature function, as I have seen an example on how to do this using that function but cant make that work for what I am doing here.



The map works great without trying to do a different color icon. I have all my points and polygons appearing and controlable by the layercontrol, but when I try to add the point to layer I just get a white map or the same map with no changes depending on where I try to add it in.



I added the icon sections below based off of the Leaflet Tutorial -



 var routeca1 = L.layerGroup();

var geojsonFeature1 = [{***random geojson data points here****} ];

L.geoJSON(geojsonFeature1)
.bindPopup(function (layer) {
return '<b>'+'Location ID: '+ layer.feature.properties.locationid +'</b>' + '<br/>' + '<hr />'+ layer.feature.properties.Description + '<br/>' + layer.feature.properties.model + '<br/>' + '<hr />'+ layer.feature.properties.addresscomponents;
}).addTo(routeca1);

var streets = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>', maxZoom: 17, minZoom: 6});

var map = L.map('map', {
center: [34.068, -118.248],
zoom: 10,
layers: [streets, routeca1]
});

var baseLayers = {
"Streets": streets
};

var overlays = {
"CA1": routeca1
};

var MarkerIcon = L.Icon.extend({
options: {
shadowUrl: './leaflet/images/marker-shadow.png',
iconSize: [38, 95],
shadowSize: [50, 64],
iconAnchor: [22, 94],
shadowAnchor: [4, 62],
popupAnchor: [-3, -76]
}
});

var greenIcon = new MarkerIcon({iconUrl: './leaflet/images/marker-icon-green.png'}),
redIcon = new MarkerIcon({iconUrl: './leaflet/images/marker-icon-red.png'}),
blueIcon = new MarkerIcon({iconUrl: './leaflet/images/marker-blue.png'});

L.icon = function (options) {
return new L.Icon(options);
};

L.control.layers(baseLayers, overlays).addTo(map);









share|improve this question





























    2















    I have searched posts and tutorials and could not find the answer to my specific application of this feature



    Almost all the examples use L.marker to simply place a marker on the map using various custom icons. This would be fine except I am not just placing markers but rather adding all the geoJson data to layers, then adding those layergroups to the control and then to the map. - I.E. I want all the layer routeca1 to use a red icon. I think I have narrowed this to needing to use the point to layer function:



    pointToLayer: function (feature, latlng) {
    return L.marker(latlng, {icon: redIcon});
    }


    I am not using the onEachFeature function, as I have seen an example on how to do this using that function but cant make that work for what I am doing here.



    The map works great without trying to do a different color icon. I have all my points and polygons appearing and controlable by the layercontrol, but when I try to add the point to layer I just get a white map or the same map with no changes depending on where I try to add it in.



    I added the icon sections below based off of the Leaflet Tutorial -



     var routeca1 = L.layerGroup();

    var geojsonFeature1 = [{***random geojson data points here****} ];

    L.geoJSON(geojsonFeature1)
    .bindPopup(function (layer) {
    return '<b>'+'Location ID: '+ layer.feature.properties.locationid +'</b>' + '<br/>' + '<hr />'+ layer.feature.properties.Description + '<br/>' + layer.feature.properties.model + '<br/>' + '<hr />'+ layer.feature.properties.addresscomponents;
    }).addTo(routeca1);

    var streets = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>', maxZoom: 17, minZoom: 6});

    var map = L.map('map', {
    center: [34.068, -118.248],
    zoom: 10,
    layers: [streets, routeca1]
    });

    var baseLayers = {
    "Streets": streets
    };

    var overlays = {
    "CA1": routeca1
    };

    var MarkerIcon = L.Icon.extend({
    options: {
    shadowUrl: './leaflet/images/marker-shadow.png',
    iconSize: [38, 95],
    shadowSize: [50, 64],
    iconAnchor: [22, 94],
    shadowAnchor: [4, 62],
    popupAnchor: [-3, -76]
    }
    });

    var greenIcon = new MarkerIcon({iconUrl: './leaflet/images/marker-icon-green.png'}),
    redIcon = new MarkerIcon({iconUrl: './leaflet/images/marker-icon-red.png'}),
    blueIcon = new MarkerIcon({iconUrl: './leaflet/images/marker-blue.png'});

    L.icon = function (options) {
    return new L.Icon(options);
    };

    L.control.layers(baseLayers, overlays).addTo(map);









    share|improve this question



























      2












      2








      2








      I have searched posts and tutorials and could not find the answer to my specific application of this feature



      Almost all the examples use L.marker to simply place a marker on the map using various custom icons. This would be fine except I am not just placing markers but rather adding all the geoJson data to layers, then adding those layergroups to the control and then to the map. - I.E. I want all the layer routeca1 to use a red icon. I think I have narrowed this to needing to use the point to layer function:



      pointToLayer: function (feature, latlng) {
      return L.marker(latlng, {icon: redIcon});
      }


      I am not using the onEachFeature function, as I have seen an example on how to do this using that function but cant make that work for what I am doing here.



      The map works great without trying to do a different color icon. I have all my points and polygons appearing and controlable by the layercontrol, but when I try to add the point to layer I just get a white map or the same map with no changes depending on where I try to add it in.



      I added the icon sections below based off of the Leaflet Tutorial -



       var routeca1 = L.layerGroup();

      var geojsonFeature1 = [{***random geojson data points here****} ];

      L.geoJSON(geojsonFeature1)
      .bindPopup(function (layer) {
      return '<b>'+'Location ID: '+ layer.feature.properties.locationid +'</b>' + '<br/>' + '<hr />'+ layer.feature.properties.Description + '<br/>' + layer.feature.properties.model + '<br/>' + '<hr />'+ layer.feature.properties.addresscomponents;
      }).addTo(routeca1);

      var streets = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>', maxZoom: 17, minZoom: 6});

      var map = L.map('map', {
      center: [34.068, -118.248],
      zoom: 10,
      layers: [streets, routeca1]
      });

      var baseLayers = {
      "Streets": streets
      };

      var overlays = {
      "CA1": routeca1
      };

      var MarkerIcon = L.Icon.extend({
      options: {
      shadowUrl: './leaflet/images/marker-shadow.png',
      iconSize: [38, 95],
      shadowSize: [50, 64],
      iconAnchor: [22, 94],
      shadowAnchor: [4, 62],
      popupAnchor: [-3, -76]
      }
      });

      var greenIcon = new MarkerIcon({iconUrl: './leaflet/images/marker-icon-green.png'}),
      redIcon = new MarkerIcon({iconUrl: './leaflet/images/marker-icon-red.png'}),
      blueIcon = new MarkerIcon({iconUrl: './leaflet/images/marker-blue.png'});

      L.icon = function (options) {
      return new L.Icon(options);
      };

      L.control.layers(baseLayers, overlays).addTo(map);









      share|improve this question
















      I have searched posts and tutorials and could not find the answer to my specific application of this feature



      Almost all the examples use L.marker to simply place a marker on the map using various custom icons. This would be fine except I am not just placing markers but rather adding all the geoJson data to layers, then adding those layergroups to the control and then to the map. - I.E. I want all the layer routeca1 to use a red icon. I think I have narrowed this to needing to use the point to layer function:



      pointToLayer: function (feature, latlng) {
      return L.marker(latlng, {icon: redIcon});
      }


      I am not using the onEachFeature function, as I have seen an example on how to do this using that function but cant make that work for what I am doing here.



      The map works great without trying to do a different color icon. I have all my points and polygons appearing and controlable by the layercontrol, but when I try to add the point to layer I just get a white map or the same map with no changes depending on where I try to add it in.



      I added the icon sections below based off of the Leaflet Tutorial -



       var routeca1 = L.layerGroup();

      var geojsonFeature1 = [{***random geojson data points here****} ];

      L.geoJSON(geojsonFeature1)
      .bindPopup(function (layer) {
      return '<b>'+'Location ID: '+ layer.feature.properties.locationid +'</b>' + '<br/>' + '<hr />'+ layer.feature.properties.Description + '<br/>' + layer.feature.properties.model + '<br/>' + '<hr />'+ layer.feature.properties.addresscomponents;
      }).addTo(routeca1);

      var streets = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>', maxZoom: 17, minZoom: 6});

      var map = L.map('map', {
      center: [34.068, -118.248],
      zoom: 10,
      layers: [streets, routeca1]
      });

      var baseLayers = {
      "Streets": streets
      };

      var overlays = {
      "CA1": routeca1
      };

      var MarkerIcon = L.Icon.extend({
      options: {
      shadowUrl: './leaflet/images/marker-shadow.png',
      iconSize: [38, 95],
      shadowSize: [50, 64],
      iconAnchor: [22, 94],
      shadowAnchor: [4, 62],
      popupAnchor: [-3, -76]
      }
      });

      var greenIcon = new MarkerIcon({iconUrl: './leaflet/images/marker-icon-green.png'}),
      redIcon = new MarkerIcon({iconUrl: './leaflet/images/marker-icon-red.png'}),
      blueIcon = new MarkerIcon({iconUrl: './leaflet/images/marker-blue.png'});

      L.icon = function (options) {
      return new L.Icon(options);
      };

      L.control.layers(baseLayers, overlays).addTo(map);






      leaflet geojson layers mapping icon






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 18 mins ago









      PolyGeo

      53.6k1780240




      53.6k1780240










      asked Jul 12 '18 at 15:51









      eric47905eric47905

      257




      257






















          1 Answer
          1






          active

          oldest

          votes


















          1














          I edited your code just using the red leaf from this example. The key changes I made to your code are:




          1. declaring the icon variable(s) before you turn your geoJSON data into a leaflet layer. That way, you can refer to the icon variable during the layer's construction.

          2. adding the "point to layer" code to the L.geoJSON layer's construction


          3. giving the leaflet layer a variable name (myLayer) so that I can set the icon and bind the popup separately.



            //I start by setting up my icon here
            var MarkerIcon = L.Icon.extend({
            options: {
            shadowUrl: 'leaf-shadow.png',
            iconSize: [38, 95],
            shadowSize: [50, 64],
            iconAnchor: [22, 94],
            shadowAnchor: [4, 62],
            popupAnchor: [-3, -76]
            }
            });



            //be sure the png files exist in your file directory and are pathed correctly
            var greenIcon = new MarkerIcon({iconUrl: 'leaf-green.png'}),
            redIcon = new MarkerIcon({iconUrl: 'leaf-red.png'}),
            orangeIcon = new MarkerIcon({iconUrl: 'leaf-orange.png'});



            L.icon = function (options) {
            return new L.Icon(options);
            };



            // I create an empty layer group
            var routeca1 = L.layerGroup();



            //var geojsonFeature1 = [{***random geojson data points here****} ];
            //I made up some points which work in your set-up. Though most of the attributes for the pop-up are missing
            var geojsonFeature1 = {
            "type": "FeatureCollection",
            "features": [
            {
            "type": "Feature",
            "properties": {
            "Description": "A"
            },
            "geometry": {
            "type": "Point",
            "coordinates": [
            -118.24207305908203,
            34.084369762367984
            ]
            }
            },
            {
            "type": "Feature",
            "properties": {
            "Description": "B"
            },
            "geometry": {
            "type": "Point",
            "coordinates": [
            -118.23057174682616,
            34.07356398381666
            ]
            }
            }
            ]
            };



            // L.geoJSON(geojsonFeature1); //removed this line
            //and added the following instead, which sets the icon based on the variable created above and adds each marker to the layer group. Note "redIcon" must be created above this, or it is not going to work:
            myLayer = L.geoJSON(geojsonFeature1, {
            pointToLayer: function (feature, latlng){
            return L.marker(latlng, {icon: redIcon});
            }
            }).addTo(routeca1);



            //I then bind the popup to the layer here:
            myLayer.bindPopup(function (layer) {
            return ''+'Location ID: '+ layer.feature.properties.locationid +'' + '
            ' + '



            '+ layer.feature.properties.Description + '
            ' + layer.feature.properties.model + '
            ' + '
            '+ layer.feature.properties.addresscomponents;
            });

            // now I create my map and other layers
            var streets = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {attribution: '© OpenStreetMap', maxZoom: 17, minZoom: 6});



            var map = L.map('map', {
            center: [34.068, -118.248],
            zoom: 10,
            layers: [streets]
            });



            var baseLayers = {
            "Streets": streets
            };



            var overlays = {
            "CA1": routeca1,



            };


            //lastly add the layers to the map!
            L.control.layers(baseLayers, overlays).addTo(map);








          share|improve this answer


























          • Thank you so much for the help, and more for the explanation, It makes sense when you describe it like that. I will give it a shot in the morning and update the post once I give it a go.

            – eric47905
            Jul 12 '18 at 23:15






          • 1





            Sorry forgot to update, this worked great. Thanks again!

            – eric47905
            Jan 1 at 21:28











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "79"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f289284%2fusing-custom-icons-for-leaflet-layer-groups%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          I edited your code just using the red leaf from this example. The key changes I made to your code are:




          1. declaring the icon variable(s) before you turn your geoJSON data into a leaflet layer. That way, you can refer to the icon variable during the layer's construction.

          2. adding the "point to layer" code to the L.geoJSON layer's construction


          3. giving the leaflet layer a variable name (myLayer) so that I can set the icon and bind the popup separately.



            //I start by setting up my icon here
            var MarkerIcon = L.Icon.extend({
            options: {
            shadowUrl: 'leaf-shadow.png',
            iconSize: [38, 95],
            shadowSize: [50, 64],
            iconAnchor: [22, 94],
            shadowAnchor: [4, 62],
            popupAnchor: [-3, -76]
            }
            });



            //be sure the png files exist in your file directory and are pathed correctly
            var greenIcon = new MarkerIcon({iconUrl: 'leaf-green.png'}),
            redIcon = new MarkerIcon({iconUrl: 'leaf-red.png'}),
            orangeIcon = new MarkerIcon({iconUrl: 'leaf-orange.png'});



            L.icon = function (options) {
            return new L.Icon(options);
            };



            // I create an empty layer group
            var routeca1 = L.layerGroup();



            //var geojsonFeature1 = [{***random geojson data points here****} ];
            //I made up some points which work in your set-up. Though most of the attributes for the pop-up are missing
            var geojsonFeature1 = {
            "type": "FeatureCollection",
            "features": [
            {
            "type": "Feature",
            "properties": {
            "Description": "A"
            },
            "geometry": {
            "type": "Point",
            "coordinates": [
            -118.24207305908203,
            34.084369762367984
            ]
            }
            },
            {
            "type": "Feature",
            "properties": {
            "Description": "B"
            },
            "geometry": {
            "type": "Point",
            "coordinates": [
            -118.23057174682616,
            34.07356398381666
            ]
            }
            }
            ]
            };



            // L.geoJSON(geojsonFeature1); //removed this line
            //and added the following instead, which sets the icon based on the variable created above and adds each marker to the layer group. Note "redIcon" must be created above this, or it is not going to work:
            myLayer = L.geoJSON(geojsonFeature1, {
            pointToLayer: function (feature, latlng){
            return L.marker(latlng, {icon: redIcon});
            }
            }).addTo(routeca1);



            //I then bind the popup to the layer here:
            myLayer.bindPopup(function (layer) {
            return ''+'Location ID: '+ layer.feature.properties.locationid +'' + '
            ' + '



            '+ layer.feature.properties.Description + '
            ' + layer.feature.properties.model + '
            ' + '
            '+ layer.feature.properties.addresscomponents;
            });

            // now I create my map and other layers
            var streets = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {attribution: '© OpenStreetMap', maxZoom: 17, minZoom: 6});



            var map = L.map('map', {
            center: [34.068, -118.248],
            zoom: 10,
            layers: [streets]
            });



            var baseLayers = {
            "Streets": streets
            };



            var overlays = {
            "CA1": routeca1,



            };


            //lastly add the layers to the map!
            L.control.layers(baseLayers, overlays).addTo(map);








          share|improve this answer


























          • Thank you so much for the help, and more for the explanation, It makes sense when you describe it like that. I will give it a shot in the morning and update the post once I give it a go.

            – eric47905
            Jul 12 '18 at 23:15






          • 1





            Sorry forgot to update, this worked great. Thanks again!

            – eric47905
            Jan 1 at 21:28
















          1














          I edited your code just using the red leaf from this example. The key changes I made to your code are:




          1. declaring the icon variable(s) before you turn your geoJSON data into a leaflet layer. That way, you can refer to the icon variable during the layer's construction.

          2. adding the "point to layer" code to the L.geoJSON layer's construction


          3. giving the leaflet layer a variable name (myLayer) so that I can set the icon and bind the popup separately.



            //I start by setting up my icon here
            var MarkerIcon = L.Icon.extend({
            options: {
            shadowUrl: 'leaf-shadow.png',
            iconSize: [38, 95],
            shadowSize: [50, 64],
            iconAnchor: [22, 94],
            shadowAnchor: [4, 62],
            popupAnchor: [-3, -76]
            }
            });



            //be sure the png files exist in your file directory and are pathed correctly
            var greenIcon = new MarkerIcon({iconUrl: 'leaf-green.png'}),
            redIcon = new MarkerIcon({iconUrl: 'leaf-red.png'}),
            orangeIcon = new MarkerIcon({iconUrl: 'leaf-orange.png'});



            L.icon = function (options) {
            return new L.Icon(options);
            };



            // I create an empty layer group
            var routeca1 = L.layerGroup();



            //var geojsonFeature1 = [{***random geojson data points here****} ];
            //I made up some points which work in your set-up. Though most of the attributes for the pop-up are missing
            var geojsonFeature1 = {
            "type": "FeatureCollection",
            "features": [
            {
            "type": "Feature",
            "properties": {
            "Description": "A"
            },
            "geometry": {
            "type": "Point",
            "coordinates": [
            -118.24207305908203,
            34.084369762367984
            ]
            }
            },
            {
            "type": "Feature",
            "properties": {
            "Description": "B"
            },
            "geometry": {
            "type": "Point",
            "coordinates": [
            -118.23057174682616,
            34.07356398381666
            ]
            }
            }
            ]
            };



            // L.geoJSON(geojsonFeature1); //removed this line
            //and added the following instead, which sets the icon based on the variable created above and adds each marker to the layer group. Note "redIcon" must be created above this, or it is not going to work:
            myLayer = L.geoJSON(geojsonFeature1, {
            pointToLayer: function (feature, latlng){
            return L.marker(latlng, {icon: redIcon});
            }
            }).addTo(routeca1);



            //I then bind the popup to the layer here:
            myLayer.bindPopup(function (layer) {
            return ''+'Location ID: '+ layer.feature.properties.locationid +'' + '
            ' + '



            '+ layer.feature.properties.Description + '
            ' + layer.feature.properties.model + '
            ' + '
            '+ layer.feature.properties.addresscomponents;
            });

            // now I create my map and other layers
            var streets = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {attribution: '© OpenStreetMap', maxZoom: 17, minZoom: 6});



            var map = L.map('map', {
            center: [34.068, -118.248],
            zoom: 10,
            layers: [streets]
            });



            var baseLayers = {
            "Streets": streets
            };



            var overlays = {
            "CA1": routeca1,



            };


            //lastly add the layers to the map!
            L.control.layers(baseLayers, overlays).addTo(map);








          share|improve this answer


























          • Thank you so much for the help, and more for the explanation, It makes sense when you describe it like that. I will give it a shot in the morning and update the post once I give it a go.

            – eric47905
            Jul 12 '18 at 23:15






          • 1





            Sorry forgot to update, this worked great. Thanks again!

            – eric47905
            Jan 1 at 21:28














          1












          1








          1







          I edited your code just using the red leaf from this example. The key changes I made to your code are:




          1. declaring the icon variable(s) before you turn your geoJSON data into a leaflet layer. That way, you can refer to the icon variable during the layer's construction.

          2. adding the "point to layer" code to the L.geoJSON layer's construction


          3. giving the leaflet layer a variable name (myLayer) so that I can set the icon and bind the popup separately.



            //I start by setting up my icon here
            var MarkerIcon = L.Icon.extend({
            options: {
            shadowUrl: 'leaf-shadow.png',
            iconSize: [38, 95],
            shadowSize: [50, 64],
            iconAnchor: [22, 94],
            shadowAnchor: [4, 62],
            popupAnchor: [-3, -76]
            }
            });



            //be sure the png files exist in your file directory and are pathed correctly
            var greenIcon = new MarkerIcon({iconUrl: 'leaf-green.png'}),
            redIcon = new MarkerIcon({iconUrl: 'leaf-red.png'}),
            orangeIcon = new MarkerIcon({iconUrl: 'leaf-orange.png'});



            L.icon = function (options) {
            return new L.Icon(options);
            };



            // I create an empty layer group
            var routeca1 = L.layerGroup();



            //var geojsonFeature1 = [{***random geojson data points here****} ];
            //I made up some points which work in your set-up. Though most of the attributes for the pop-up are missing
            var geojsonFeature1 = {
            "type": "FeatureCollection",
            "features": [
            {
            "type": "Feature",
            "properties": {
            "Description": "A"
            },
            "geometry": {
            "type": "Point",
            "coordinates": [
            -118.24207305908203,
            34.084369762367984
            ]
            }
            },
            {
            "type": "Feature",
            "properties": {
            "Description": "B"
            },
            "geometry": {
            "type": "Point",
            "coordinates": [
            -118.23057174682616,
            34.07356398381666
            ]
            }
            }
            ]
            };



            // L.geoJSON(geojsonFeature1); //removed this line
            //and added the following instead, which sets the icon based on the variable created above and adds each marker to the layer group. Note "redIcon" must be created above this, or it is not going to work:
            myLayer = L.geoJSON(geojsonFeature1, {
            pointToLayer: function (feature, latlng){
            return L.marker(latlng, {icon: redIcon});
            }
            }).addTo(routeca1);



            //I then bind the popup to the layer here:
            myLayer.bindPopup(function (layer) {
            return ''+'Location ID: '+ layer.feature.properties.locationid +'' + '
            ' + '



            '+ layer.feature.properties.Description + '
            ' + layer.feature.properties.model + '
            ' + '
            '+ layer.feature.properties.addresscomponents;
            });

            // now I create my map and other layers
            var streets = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {attribution: '© OpenStreetMap', maxZoom: 17, minZoom: 6});



            var map = L.map('map', {
            center: [34.068, -118.248],
            zoom: 10,
            layers: [streets]
            });



            var baseLayers = {
            "Streets": streets
            };



            var overlays = {
            "CA1": routeca1,



            };


            //lastly add the layers to the map!
            L.control.layers(baseLayers, overlays).addTo(map);








          share|improve this answer















          I edited your code just using the red leaf from this example. The key changes I made to your code are:




          1. declaring the icon variable(s) before you turn your geoJSON data into a leaflet layer. That way, you can refer to the icon variable during the layer's construction.

          2. adding the "point to layer" code to the L.geoJSON layer's construction


          3. giving the leaflet layer a variable name (myLayer) so that I can set the icon and bind the popup separately.



            //I start by setting up my icon here
            var MarkerIcon = L.Icon.extend({
            options: {
            shadowUrl: 'leaf-shadow.png',
            iconSize: [38, 95],
            shadowSize: [50, 64],
            iconAnchor: [22, 94],
            shadowAnchor: [4, 62],
            popupAnchor: [-3, -76]
            }
            });



            //be sure the png files exist in your file directory and are pathed correctly
            var greenIcon = new MarkerIcon({iconUrl: 'leaf-green.png'}),
            redIcon = new MarkerIcon({iconUrl: 'leaf-red.png'}),
            orangeIcon = new MarkerIcon({iconUrl: 'leaf-orange.png'});



            L.icon = function (options) {
            return new L.Icon(options);
            };



            // I create an empty layer group
            var routeca1 = L.layerGroup();



            //var geojsonFeature1 = [{***random geojson data points here****} ];
            //I made up some points which work in your set-up. Though most of the attributes for the pop-up are missing
            var geojsonFeature1 = {
            "type": "FeatureCollection",
            "features": [
            {
            "type": "Feature",
            "properties": {
            "Description": "A"
            },
            "geometry": {
            "type": "Point",
            "coordinates": [
            -118.24207305908203,
            34.084369762367984
            ]
            }
            },
            {
            "type": "Feature",
            "properties": {
            "Description": "B"
            },
            "geometry": {
            "type": "Point",
            "coordinates": [
            -118.23057174682616,
            34.07356398381666
            ]
            }
            }
            ]
            };



            // L.geoJSON(geojsonFeature1); //removed this line
            //and added the following instead, which sets the icon based on the variable created above and adds each marker to the layer group. Note "redIcon" must be created above this, or it is not going to work:
            myLayer = L.geoJSON(geojsonFeature1, {
            pointToLayer: function (feature, latlng){
            return L.marker(latlng, {icon: redIcon});
            }
            }).addTo(routeca1);



            //I then bind the popup to the layer here:
            myLayer.bindPopup(function (layer) {
            return ''+'Location ID: '+ layer.feature.properties.locationid +'' + '
            ' + '



            '+ layer.feature.properties.Description + '
            ' + layer.feature.properties.model + '
            ' + '
            '+ layer.feature.properties.addresscomponents;
            });

            // now I create my map and other layers
            var streets = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {attribution: '© OpenStreetMap', maxZoom: 17, minZoom: 6});



            var map = L.map('map', {
            center: [34.068, -118.248],
            zoom: 10,
            layers: [streets]
            });



            var baseLayers = {
            "Streets": streets
            };



            var overlays = {
            "CA1": routeca1,



            };


            //lastly add the layers to the map!
            L.control.layers(baseLayers, overlays).addTo(map);









          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 17 mins ago









          PolyGeo

          53.6k1780240




          53.6k1780240










          answered Jul 12 '18 at 23:06









          JMersJMers

          1458




          1458













          • Thank you so much for the help, and more for the explanation, It makes sense when you describe it like that. I will give it a shot in the morning and update the post once I give it a go.

            – eric47905
            Jul 12 '18 at 23:15






          • 1





            Sorry forgot to update, this worked great. Thanks again!

            – eric47905
            Jan 1 at 21:28



















          • Thank you so much for the help, and more for the explanation, It makes sense when you describe it like that. I will give it a shot in the morning and update the post once I give it a go.

            – eric47905
            Jul 12 '18 at 23:15






          • 1





            Sorry forgot to update, this worked great. Thanks again!

            – eric47905
            Jan 1 at 21:28

















          Thank you so much for the help, and more for the explanation, It makes sense when you describe it like that. I will give it a shot in the morning and update the post once I give it a go.

          – eric47905
          Jul 12 '18 at 23:15





          Thank you so much for the help, and more for the explanation, It makes sense when you describe it like that. I will give it a shot in the morning and update the post once I give it a go.

          – eric47905
          Jul 12 '18 at 23:15




          1




          1





          Sorry forgot to update, this worked great. Thanks again!

          – eric47905
          Jan 1 at 21:28





          Sorry forgot to update, this worked great. Thanks again!

          – eric47905
          Jan 1 at 21:28


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f289284%2fusing-custom-icons-for-leaflet-layer-groups%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          (145452) 2005 RN43 Классификация | Примечания | Ссылки |...

          Щит и меч (фильм) Содержание Названия серий | Сюжет |...

          Энтрерриос (город) Содержание История | Географическое...