Markers With Custom Icons - Leaflet - a JavaScript library for interactive maps
Markers With Custom Icons - Leaflet - a JavaScript library for interactive maps
← Tutorials
+
−
Creating an icon
Marker icons in Leaflet are defined by L.Icon objects, which are passed as an option when creating markers. Let’s
create a green leaf icon:
+
−
Now we can create all three of our leaf icons from this class and use them:
You may have noticed that we used the new keyword for creating LeafIcon instances. So why do all Leaflet classes
get created without it? The answer is simple: the real Leaflet classes are named with a capital letter (e.g. L.Icon),
and they also need to be created with new, but there are also shortcuts with lowercase names (L.icon), created for
convenience like this:
You can do the same with your classes too. OK, let’s finally put some markers with these icons on the map:
L.marker([51.5, -0.09], {icon: greenIcon}).addTo(map).bindPopup("I am a green leaf.");
L.marker([51.495, -0.083], {icon: redIcon}).addTo(map).bindPopup("I am a red leaf.");
L.marker([51.49, -0.1], {icon: orangeIcon}).addTo(map).bindPopup("I am an orange leaf.");
That’s it. Now take a look at the full example, the L.Icon docs, or browse other examples.