Search code examples
leafletleaflet.draw

Add custom icons to Leaflet Draw toolbar


Note: Simular post to change default icon toolbar (Leaflet).

I am trying to add a custom icon for the leaflet Draw toolbar. But i can't seem to figure out how.

Leaflet draw toolbar

As seen on the image, I have two markers. The goal is to change one of the icons in the toolbar. My code is the following:

L.DrawToolbar.include({
          getModeHandlers: function (map) {
              return [          
                  {
                      enabled: true,
                      handler: new L.Draw.Polyline(map, { metric: true, repeatMode: true }),
                      title: '...'
                  },
                  {
                      enabled: true,
                      handler: new L.Draw.Polygon(map, { allowIntersection: false, showArea: true, metric: true, repeatMode: false }),
                      title: '...'
                  },
                  {
                      enabled: true,
                      handler: new L.Draw.Marker(map, { icon: new L.Icon.Default() }),
                      title: '...'
                  },
                  {
                      enabled: true,
                      handler: new L.Draw.Marker(map, { icon: new L.Icon.Default() }),
                      title: '...'
                  }
              ];
          }
      });

Solution

  • As you can see here with inspect element there is this CSS property :

    .leaflet-draw-toolbar a {
        background-image: url(images/spritesheet.png);
        background-repeat: no-repeat;
    }
    

    Spritesheet is :

    Spritesheet

    You can simply edit background-image: url(); with the URL of your icon on the a element you want (example for polygon one):

    .leaflet-draw-toolbar .leaflet-draw-draw-polygon {
        background: url(data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='20' height='20'><circle cx='5' cy='5' r='5' /></svg>) no-repeat;
        background-color: #FFF;
    }
    

    You can add SVG, image or content.