constituencyMapExperimentClickable = {
const container = yield htl.html`<div style="height: 600px;">`;
const map = L.map(container);
const constituencylayer = L.geoJSON(constituenciesFromAPI).addTo(map);
function onLocationFound(e) {
var userLocation = e.latlng;
var radius = e.accuracy;
var userConstituency = "Unknown Constituency";
constituenciesFromAPI.features.forEach((feature) => {
const layer = L.geoJSON(feature);
layer.eachLayer(function (polygonLayer) {
if (
polygonLayer instanceof L.Polygon &&
polygonLayer.getBounds().contains(userLocation)
) {
if (L.GeometryUtil.inside(userLocation, polygonLayer)) {
userConstituency = feature.properties.ENG_NAME_VALUE;
}
}
});
});
L.marker(userLocation)
.addTo(map)
.bindPopup(`You are here in ${userConstituency}`)
.openPopup();
L.circle(userLocation, radius).addTo(map);
}
function onEachFeature(feature, layer) {
if (feature.properties && feature.properties.ENG_NAME_VALUE) {
layer
.bindTooltip(feature.properties.ENG_NAME_VALUE.slice(0, -4))
.openPopup(feature.properties.ENG_NAME_VALUE.slice(0, -4));
}
}
function myStyle(feature) {
return {
fillColor: "yellow",
weight: 2,
opacity: 1,
color: "grey",
dashArray: 1,
fillOpacity: 0.1
};
}
function highlightFeature(e) {
var layer = e.target;
layer.setStyle({
weight: 1,
color: "",
dashArray: "",
fillOpacity: 0.01
});
if (!L.Browser.ie && !L.Browser.opera && !L.Browser.edge) {
layer.bringToFront();
}
}
function resetHighlight(e) {
constituenciesFromAPI.resetStyle(e.target);
}
map.on("locationfound", onLocationFound);
L.geoJSON(constituenciesFromAPI, {
style: myStyle,
onEachFeature: onEachFeature,
filter: function (feature, layer) {
return feature.properties.ENG_NAME_VALUE;
}
}).addTo(map);
map.fitBounds(constituencylayer.getBounds(), { maxZoom: 19 });
map.locate({ setView: true, maxZoom: 9 });
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution:
"© <a href=https://www.openstreetmap.org/copyright>OpenStreetMap</a> contributors"
}).addTo(map);
}