blog/layouts/shortcodes/leaflet-topojson.html

30 lines
744 B
HTML
Raw Permalink Normal View History

2019-04-13 16:00:44 +00:00
<script>
// Changing topojson to geojson
L.TopoJSON = L.GeoJSON.extend({
addData: function(jsonData) {
if (jsonData.type === "Topology") {
for (let key in jsonData.objects) {
if (jsonData.objects.hasOwnProperty(key)) {
let geojson = topojson.feature(jsonData, jsonData.objects[key]);
L.GeoJSON.prototype.addData.call(this, geojson);
}
}
} else {
L.GeoJSON.prototype.addData.call(this, jsonData);
}
}
});
const topoLayer = new L.TopoJSON();
fetch('{{ .Get "url" | plainify }}')
.then(response => {
return response.json();
})
.then(data => {
topoLayer.addData(data);
topoLayer.addTo(mymap);
});
</script>