index.html
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>TileLayer and Geo-Projections - Tilelayer force reload</title>
<style type="text/css">
html,body{margin:0px;height:100%;width:100%}
.container{width:100%;height:100%}
.panel{border-radius:5px;padding:10px;position:absolute;z-index:1;right:10px;top:10px;border:1px solid gray;background-color:white}
.panel .item{line-height:40px}
</style>
<link rel="stylesheet" href="https://unpkg.com/maptalks/dist/maptalks.css">
<script type="text/javascript" src="https://unpkg.com/maptalks/dist/maptalks.min.js"></script>
<body>
<div class="panel">
<div class="item">
<button onclick="reload()">reload tilelayer</button>
</div>
</div>
<div id="map" class="container"></div>
<script>
var map = new maptalks.Map('map', {
center: [-100.43074996462228, 37.69212817202581],
zoom: 4,
spatialReference: {
projection: 'EPSG:4326'
},
baseLayer: new maptalks.WMSTileLayer('wms', {
'tileSystem': [1, -1, -180, 90],
'urlTemplate': 'https://ows.terrestris.de/osm/service',
'crs': 'EPSG:4326',
'layers': 'OSM-WMS',
'styles': '',
'version': '1.3.0',
'format': 'image/png',
'transparent': true,
'uppercase': true
}),
attribution: {
content: '© ows.terrestris.de'
}
});
var isTerrestris = true;
function reload() {
var baseLayer = map.getBaseLayer();
var options = baseLayer.options;
if (!isTerrestris) {
options.urlTemplate = 'https://ows.terrestris.de/osm/service';
options.layers = 'OSM-WMS';
}
else {
options.urlTemplate = 'https://ahocevar.com/geoserver/wms';
options.layers = 'topp:states';
}
baseLayer.forceReload();
isTerrestris = !isTerrestris;
}
</script>
</body>
</html>