Matrix routing visualised in MapLibre

by Mapycz

HTML

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Routing visualised in MapLibre</title>
    <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
    <script src="https://unpkg.com/maplibre-gl@2.4.0/dist/maplibre-gl.js"></script>
    <link href="https://unpkg.com/maplibre-gl@2.4.0/dist/maplibre-gl.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%;
}

div.maplibregl-popup {
  opacity: 0;
  transition: opacity 500ms ease-in-out;
}

div.maplibregl-popup.visible {
  opacity: 1;
}

div.maplibregl-popup-content {
  padding: 8px;
}

.maplibregl-popup-content p {
  margin: 0;
  padding: 0;
}

.maplibregl-popup-close-button {
  display: none;
}

.restartButton {
  font-family: Arial, sans-serif;
  position: absolute;
  top: 24px;
  left: 24px;
  padding: 16px;
  font-size: 24px;
  border-radius: 4px;
  border: 1px solid #404040;
  background: #ffffff;
  color: #404040;
  cursor: pointer;
}

.restartButton:hover,
.restartButton:active {
  background: #f0f0f0;
}

JavaScript

// replace with your own API key
const API_KEY = 'eyJpIjoyNTcsImMiOjE2Njc0ODU2MjN9.c_UlvdpHGTI_Jb-TNMYlDYuIkCLJaUpi911RdlwPsAY';
const matrixPoints = [
	[14.4446972, 50.0923697],
  [14.3895244, 50.1023542],
  [14.3458714, 50.0912031],
  [14.3596150, 50.0536933],
  [14.4213917, 50.0432764],
  [14.4601119, 50.0706642],
];
const destination = [14.4009131, 50.0711367];
const map = new maplibregl.Map({
	container: 'map',
	center: destination,
	zoom: 10,
	style: {
		version: 8,
		sources: {
    	// style for map tiles
			'basic-tiles': {
				type: 'raster',
				url: `https://api.mapy.cz/v1/maptiles/basic/tiles.json?apikey=${API_KEY}`,
				tileSize: 256,
			},
      // style for our geometry
      'route-geometry': {
        type: 'geojson',
        data: {
          type: "LineString",
          coordinates: [],
        },
      },
		},
		layers: [{
			id: 'tiles',
			type: 'raster',
			source: 'basic-tiles',
		}, {
      id: 'route-geometry',
      type: 'line',
      source: 'route-geometry',
      layout: {
        'line-join': 'round',
        'line-cap': 'round',
      },
      paint: {
        'line-color': '#0033ff',
        'line-width': 8,
        'line-opacity': 0.6,
      },
    }],
	},
});
const restartButton = document.createElement('button');

restartButton.innerHTML = 'Restart';
restartButton.type = 'button';
restartButton.className = 'restartButton';

/*
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://maplibre.org/maplibre-gl-js-docs/api/markers/#icontrol
*/
class LogoControl {
	onAdd(map) {
		this._map = map;
		this._container = document.createElement('div');
		this._container.className = 'maplibregl-ctrl';
		this._container.innerHTML = '<a href="http://mapy.cz/" target="_blank"><img  width="100px" src="https://api.mapy.cz/img/api/logo.svg" ></a>';

		return this._container;
	}
 
	onRemove()...