Hi everyone,
I am trying to create a point based Leaflet map, which brings in data from a geoJSON file where points are coloured according to varying attributes and what I am trying to do is to create a legend for it.
From a different post I appreciate that the legend that is created on the Choropleth example on the Leaflet page is static, but i'm not yet understanding how to create a legend based on a string, rather than an integer or decimal.
I am also trying to create my map from scratch, rather than trying to butcher the Choropleth example!
So I have imported my geoJson and have styled my points as circles and have colour coded them according to the attributes contained within the 'TypeOfIssue' field thanks to help from this post:
Does anyone have any recommendations for how I can create a legend from this point? I have tried modifying the legend code from the Choropleth example with no joy and there doesn't seem to be anything else out there that helps. Here is the code that I have used after I have brought in the geoJSON:
L.geoJson(RRData, {
pointToLayer: function (feature, latlng) {
var popupOptions = { maxWith: 300 };
var popupContent = "<b>Location: </b>" + feature.properties.ActualLocation + "</br>" + "<b>Issue: </b>" + feature.properties.TypeOfIssue + "</br>" + "<b>Information: </b>" + feature.properties.Information;
//Style circles based on JSON attributes
function getOptions(properties) {
if (properties.TypeOfIssue === "Road Surface") {
return {
radius: 7,
color: "black",
fillColor: "#de2d26",
fillOpacity: 1
};
} else if (properties.TypeOfIssue === "Signage") {
return {
radius: 7,
color: "black",
fillColor: "#377eb8",
fillOpacity: 1
};
} else if (properties.TypeOfIssue === "Line Markings") {
return {
radius: 7,
color: "black",
fillColor: "#4daf4a",
fillOpacity: 1
};
} else if (properties.TypeOfIssue === "Roadside Hazards") {
return {
radius: 7,
color: "black",
fillColor: "#984ea3",
fillOpacity: 1
};
} else {
return {
radius: 7,
color: "black",
fillColor: "#ff7f00",
fillOpacity: 1
};
}
}
return L.circleMarker(latlng, getOptions(feature.properties)).bindPopup(popupContent, popupOptions);
}
}).addTo(map);