OpenLayers example

by Mapycz

HTML

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Simple map in OpenLayers</title>
    <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
    <script src="https://unpkg.com/ol@7.1.0/dist/ol.js"></script>
    <link href="https://unpkg.com/ol@7.1.0/ol.css" rel="stylesheet" />
  </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';

/*
We create the map with one tile layer 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: [
		new ol.layer.Tile({
			source: new ol.source.TileJSON({
				url: `https://api.mapy.com/v1/maptiles/basic/tiles.json?apikey=${API_KEY}`,
			}),
		}),
	],
	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 {
  constructor(opt_options) {
    const options = opt_options || {};
    const element = document.createElement('div');

    element.className = 'logocontrol ol-unselectable ol-control';
    element.innerHTML = '<a href="http://mapy.com/" target="_blank"><img src="https://api.mapy.com/img/api/logo.svg" ></a>';

    super({
      element: element,
      target: options.target,
    });
  }
}

// finally we add our LogoControl to the map
map.addControl(new LogoControl());