Add Legend to Leaflet Map
The legend does not depend on the existence of any particular layer. The part that you are missing here is a getColor
function. Copying the one from the tutorial in question:
function getColor(d) {
return d > 1000 ? '#800026' :
d > 500 ? '#BD0026' :
d > 200 ? '#E31A1C' :
d > 100 ? '#FC4E2A' :
d > 50 ? '#FD8D3C' :
d > 20 ? '#FEB24C' :
d > 10 ? '#FED976' :
'#FFEDA0';
}
You see that the breaks are the same as the grades
specified in the legend control:
grades = [0, 10, 20, 50, 100, 200, 500, 1000]
If you adjust these breakpoints to your needs and set the colors accordingly, you will have the legend you want.