diff --git a/public/resources/elevation-control.js b/public/resources/elevation-control.js new file mode 100644 index 0000000..a1fda61 --- /dev/null +++ b/public/resources/elevation-control.js @@ -0,0 +1,51 @@ +class ElevationInfoControl { + constructor(options) { + this.url = options["url"]; + } + + getDefaultPosition() { + const defaultPosition = "bottom-left"; + return defaultPosition; + } + + onAdd(map) { + this.map = map; + this.controlContainer = document.createElement("div"); + this.controlContainer.classList.add("maplibregl-ctrl"); + this.controlContainer.classList.add("maplibregl-ctrl-group"); + this.controlContainer.classList.add("maplibre-ctrl-elevation"); + this.controlContainer.textContent = "Elevation: Click on Map"; + + map.on('click', (e) => { + var url = this.url; + var coord = {"z": Math.floor(map.getZoom()), "x": e.lngLat["lng"], "y": e.lngLat["lat"]}; + for(var key in coord) { + url = url.replace(new RegExp('{'+ key +'}','g'), coord[key]); + } + + let request = new XMLHttpRequest(); + request.open("GET", url, true); + request.onload = () => { + if (request.status !== 200) { + this.controlContainer.textContent = "Elevation: No value"; + } else { + this.controlContainer.textContent = `Elevation: ${JSON.parse(request.responseText).elevation} (${JSON.stringify(coord)})`; + } + } + request.send(); + }); + return this.controlContainer; + } + + onRemove() { + if ( + !this.controlContainer || + !this.controlContainer.parentNode || + !this.map + ) { + return; + } + this.controlContainer.parentNode.removeChild(this.controlContainer); + this.map = undefined; + } + }; diff --git a/public/templates/data.tmpl b/public/templates/data.tmpl index e4ac4e0..a192061 100644 --- a/public/templates/data.tmpl +++ b/public/templates/data.tmpl @@ -9,6 +9,7 @@ + {{/use_maplibre}} {{^use_maplibre}} @@ -69,6 +71,9 @@ }; {{/is_terrain}} {{#is_terrain}} + + let baseUrl = window.location.origin; + var style = { version: 8, sources: { @@ -86,11 +91,11 @@ "terrain": { "source": "terrain" }, - layers: [ + "layers": [ { "id": "background", "paint": { - {{^if is_terrainrgb}} + {{#if is_terrainrgb}} "background-color": "hsl(190, 99%, 63%)" {{else}} "background-color": "hsl(0, 100%, 25%)" @@ -118,24 +123,34 @@ maxPitch: 85, style: style }); + map.addControl(new maplibregl.NavigationControl({ visualizePitch: true, showZoom: true, showCompass: true })); {{#is_terrain}} + map.addControl( new maplibregl.TerrainControl({ source: "terrain", }) ); + + map.addControl( + new ElevationInfoControl({ + url: baseUrl + "/data/{{id}}/elevation/{z}/{x}/{y}" + }) + ); {{/is_terrain}} {{^is_terrain}} + var inspect = new MaplibreInspect({ showInspectMap: true, showInspectButton: false }); map.addControl(inspect); + map.on('styledata', function() { var layerList = document.getElementById('layerList'); layerList.innerHTML = '';