What I’m trying to do:
I can create a KML file from received data streams stored in a data table in anvil and I want to link to it from a custom HTML template. It’s either that or re-write my custom HTML to work in Anvil - either solution works for me, though I suspect that the former is easier than the latter.
What I’ve tried and what’s not working:
See the below code snippet of custom HTML - this currently adds a map layer that links out to a file “insert link here.kml” - I essentially want to create that map layer from an internally generated anvil method.
Code Sample:
# this is a formatted code snippet.
# paste your code between ```
<center style="font-style:italic; color:#888; margin: 3em;">
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>OS Maps API | Basic Map WMTS (EPSG:27700) | OpenLayers</title>
<link rel="stylesheet" href="https://labs.os.uk/public/os-api-branding/v0.3.0/os-api-branding.css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.9.0/css/ol.css" />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<div id="map"></div>
<script src="https://labs.os.uk/public/os-api-branding/v0.3.0/os-api-branding.js"></script>
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.9.0/build/ol.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.7.0/proj4.js"></script>
<script>
var apiKey = 'apikeyhere';
var serviceUrl = 'https://api.os.uk/maps/raster/v1/wmts';
var parser = new ol.format.WMTSCapabilities();
var map;
// Setup the EPSG:27700 (British National Grid) projection.
proj4.defs("EPSG:27700", "+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +towgs84=446.448,-125.157,542.06,0.15,0.247,0.842,-20.489 +units=m +no_defs");
ol.proj.proj4.register(proj4);
fetch(serviceUrl + '?key=' + apiKey + '&service=WMTS&request=GetCapabilities&version=2.0.0')
.then(response => response.text())
.then(text => {
var result = parser.read(text);
var options = ol.source.WMTS.optionsFromCapabilities(result, {
layer: 'Leisure_27700',
matrixSet: 'EPSG:27700'
});
var source = new ol.source.WMTS(options);
var layer = new ol.layer.Tile({ source: source });
// Initialize the map object.
map = new ol.Map({
layers: [ layer , new ol.layer.Vector({
source: new ol.source.Vector({
url: "insert link here.kml",
format: new ol.format.KML()
})
})
],
target: 'map',
view: new ol.View({
projection: 'EPSG:27700',
extent: [ -238375.0, 0.0, 900000.0, 1376256.0 ],
resolutions: options.tileGrid.getResolutions(),
minZoom: 0,
maxZoom: 9,
center: [ 258318, 85367 ],
zoom: 6
})
});
});
</script>
</body>
</html>
</center>
Clone link:
share a copy of your app