Add a shorcode for TopoJSON support.

This commit is contained in:
Kazuhiro MUSASHI 2019-04-14 00:00:44 +08:00
parent 7e1fc240f3
commit c1cc2467e7
2 changed files with 30 additions and 0 deletions

View File

@ -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

View File

@ -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>