Add a shorcode for TopoJSON support.
This commit is contained in:
parent
7e1fc240f3
commit
c1cc2467e7
|
@ -51,6 +51,7 @@
|
|||
{{ if .Params.leaflet }}
|
||||
<!-- Make sure you put this AFTER Leaflet's CSS -->
|
||||
<script src="https://unpkg.com/leaflet@1.4.0/dist/leaflet.js" integrity="sha512-QVftwZFqvtRNi0ZyCtsznlKSWOStnDORoefr1enyq5mVL4tmKB3S/EnC3rRJcxCPavG10IcrVGSmPh6Qw5lwrg==" crossorigin=""></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/3.0.0/topojson.min.js"></script>
|
||||
{{ end }}
|
||||
|
||||
<!-- OGP Settings
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
<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>
|
||||
|
Loading…
Reference in New Issue