Skip to content

Instantly share code, notes, and snippets.

@danielbraun
Created November 8, 2019 09:10
Show Gist options
  • Save danielbraun/35b1b480bb1e8926209bb6a6b6297b1a to your computer and use it in GitHub Desktop.
Save danielbraun/35b1b480bb1e8926209bb6a6b6297b1a to your computer and use it in GitHub Desktop.
/*global L, document*/
document.querySelectorAll("[data-leaflet]").forEach(function(el) {
var data = JSON.parse(el.dataset.leaflet),
map = L.map(el, data);
data.tileLayers.forEach(function(layer) {
L.tileLayer(layer.urlTemplate, layer).addTo(map);
});
if (data.locate) {
map.locate(data.locate);
}
if (data.controls) {
Object.entries(data.controls.forEach(function(control) {
L.control[control[0]](control[1]).addTo(map);
}));
}
if (data.markers) {
data.markers.forEach(function(marker) {
var m = (new L.Marker.SVGMarker(marker.coordinates, marker)).addTo(map);
if (marker.popup) {
m.bindPopup(marker.popup);
}
});
}
if (data.geoJSON) {
L.geoJSON(data.geoJSON, {
style: function(feature) {
return feature.style;
},
onEachFeature: function(feature, layer) {
if (feature.properties && feature.properties.popup) {
layer.bindPopup(feature.properties.popup);
}
}
}).addTo(map);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment