OpenLayers mapset switch

by Mapycz

HTML

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Tile switcher in OpenLayers</title>
    <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
    <script src="https://unpkg.com/[email protected]/dist/ol.js"></script>
    <link href="https://unpkg.com/[email protected]/ol.css" rel="stylesheet" />
    <script src="https://unpkg.com/[email protected]"></script>
    <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/ol-layerswitcher.css" />
  </head>
  <body>
    <div id="map"></div>
  </body>
</html>

CSS

body {
  margin: 0;
  padding: 0;
}

#map {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 100%;
}

.logocontrol {
  bottom: 0;
}

JavaScript

// replace with your own API key
const API_KEY = 'eyJpIjoyNTcsImMiOjE2Njc0ODU2MjN9.c_UlvdpHGTI_Jb-TNMYlDYuIkCLJaUpi911RdlwPsAY';

/*
Array of map tile sets we want to support organized in a LayerGroup
See https://openlayers.org/en/latest/apidoc/module-ol_layer_Group-LayerGroup.html
*/
const layerGroup = new ol.layer.Group({
  title: 'Tilesets',
  layers: [
  	new ol.layer.Tile({
    	title: 'Basic',
  		visible: true,
      type: 'base',
			source: new ol.source.TileJSON({
				url: `https://api.mapy.cz/v1/maptiles/basic/tiles.json?apikey=${API_KEY}`,
			}),
		}),
    new ol.layer.Tile({
    	title: 'Outdoor',
  		visible: false,
      type: 'base',
			source: new ol.source.TileJSON({
				url: `https://api.mapy.cz/v1/maptiles/outdoor/tiles.json?apikey=${API_KEY}`,
			}),
		}),
    new ol.layer.Tile({
    	title: 'Winter',
  		visible: false,
      type: 'base',
			source: new ol.source.TileJSON({
				url: `https://api.mapy.cz/v1/maptiles/winter/tiles.json?apikey=${API_KEY}`,
			}),
		}),
    new ol.layer.Tile({
    	title: 'Aerial',
  		visible: false,
      type: 'base',
			source: new ol.source.TileJSON({
				url: `https://api.mapy.cz/v1/maptiles/aerial/tiles.json?apikey=${API_KEY}`,
			}),
		}),
  ],
});

/*
We create the map with all tile layers and set its initial coordinates and zoom.
See https://openlayers.org/en/latest/apidoc/module-ol_Map-Map.html
See https://openlayers.org/en/latest/apidoc/module-ol_layer_Tile-TileLayer.html
See https://openlayers.org/en/latest/apidoc/module-ol_View-View.html
*/
const map = new ol.Map({
	layers: [layerGroup],
	target: 'map',
	view: new ol.View({
		center: ol.proj.fromLonLat([14.8981184, 49.8729317]),
		zoom: 16,
	}),
});

/*
We also require you to include our logo somewhere over the map.
We create our own map control implementing a documented interface,
that shows a clickable logo.
See https://openlayers.org/en/latest/apidoc/module-ol_control_Control-Control.html
*/
class LogoControl extends ol.control.Control {
 ...