MapLibre GL Compare
Swipe and sync between two MapLibre maps. This plugin was originally developed for Mapbox (https://github.com/mapbox/mapbox-gl-compare) and was migrated to MapLibre after Mapbox changed its license.
Code example
Examples
Full Example without the need of a token
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Swipe between maps</title>
<meta
name="viewport"
content="initial-scale=1,maximum-scale=1,user-scalable=no"
/>
<script src="../.env"></script>
<script src="https://unpkg.com/maplibre-gl@2.1.6/dist/maplibre-gl.js"></script>
<link
href="https://unpkg.com/maplibre-gl@2.1.6/dist/maplibre-gl.css"
rel="stylesheet"
/>
<style>
body {
margin: 0;
padding: 0;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
</style>
</head>
<body>
<style>
body {
overflow: hidden;
}
body * {
-webkit-touch-callout: none;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
</style>
<script src="maplibre-gl-compare.js"></script>
<link rel="stylesheet" href="maplibre-gl-compare.css" type="text/css" />
<div id="comparison-container">
<div id="before" class="map"></div>
<div id="after" class="map"></div>
</div>
<script>
var beforeMap = new maplibregl.Map({
container: "before",
style: "https://demotiles.maplibre.org/style.json",
center: [7.221275, 50.326111],
zoom: 5,
});
var afterMap = new maplibregl.Map({
container: "after",
style:
"https://vectortiles.geo.admin.ch/styles/ch.swisstopo.leichte-basiskarte.vt/style.json",
center: [7.221275, 50.326111],
zoom: 5,
});
var container = "#comparison-container";
var map = new maplibregl.Compare(beforeMap, afterMap, container, {
});
</script>
</body>
</html>
Full Example with the need of a token
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Swipe between maps</title>
<meta
name="viewport"
content="initial-scale=1,maximum-scale=1,user-scalable=no"
/>
<script src="../.env"></script>
<script src="https://unpkg.com/maplibre-gl@2.1.6/dist/maplibre-gl.js"></script>
<link
href="https://unpkg.com/maplibre-gl@2.1.6/dist/maplibre-gl.css"
rel="stylesheet"
/>
<style>
body {
margin: 0;
padding: 0;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
</style>
</head>
<body>
<style>
body {
overflow: hidden;
}
body * {
-webkit-touch-callout: none;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
</style>
<script src="maplibre-gl-compare.js"></script>
<link rel="stylesheet" href="maplibre-gl-compare.css" type="text/css" />
<div id="comparison-container">
<div id="before" class="map"></div>
<div id="after" class="map"></div>
</div>
<script>
var beforeMap = new maplibregl.Map({
container: "before",
style:
"https://api.maptiler.com/maps/hybrid/style.json?key=get_your_own_OpIi9ZULNHzrESv6T2vL",
center: [7.221275, 50.326111],
zoom: 15,
});
var afterMap = new maplibregl.Map({
container: "after",
style:
"https://api.maptiler.com/maps/streets/style.json?key=get_your_own_OpIi9ZULNHzrESv6T2vL",
center: [7.221275, 50.326111],
zoom: 15,
});
var container = "#comparison-container";
var map = new maplibregl.Compare(beforeMap, afterMap, container, {
});
</script>
</body>
</html>
Usage
var beforeMap = new maplibregl.Map({
container: "before",
style: "https://demotiles.maplibre.org/style.json",
center: [7.221275, 50.326111],
zoom: 5,
});
var afterMap = new maplibregl.Map({
container: "after",
style:
"https://vectortiles.geo.admin.ch/styles/ch.swisstopo.leichte-basiskarte.vt/style.json",
center: [7.221275, 50.326111],
zoom: 5,
});
var container = '#comparison-container';
var map = new maplibregl.Compare(beforeMap, afterMap, container, {
mousemove: true,
orientation: 'vertical'
});
Methods
compare = new maplibregl.Compare(beforeMap, afterMap, container, {
mousemove: true,
orientation: 'vertical'
});
compare.currentPosition;
compare.setSlider(x);
compare.on('slideend', (e) => {
console.log(e.currentPosition);
});
compare.remove();
Live examples
Compare hybrid with streets
Compare swisstopo with demotiles
Installation
Native
Add tags referencing maplibre-gl-compare
after adding maplibre-gl
to your website:
<link href="https://unpkg.com/maplibre-gl@2.1.6/dist/maplibre-gl.css" rel='stylesheet' />
<link href="maplibre-gl-compare.css" rel='stylesheet' />
<script src="https://unpkg.com/maplibre-gl@2.1.6/dist/maplibre-gl.js"></script>
<script src="maplibre-gl-compare.js"></script>
Node
Motivation
This project makes it possible to easily compare two maps.
API Reference
API Reference
Bug Reports & Feature Requests
Please use the issue tracker to report any bugs or file feature requests.
Licence
ISC © MapLibre © Mapbox