Display a popup
Estimated reading time: 1 minuteCheck out this code sample that uses the MapLibre GL JS library to add a popup over your map data points.
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<link href="https://unpkg.com/[email protected]/dist/maplibre-gl.css" rel="stylesheet" />
<script src="https://unpkg.com/[email protected]/dist/maplibre-gl.js"></script>
<style>
html,
body {
margin: 0;
padding: 0;
height: 100%;
}
#map {
min-height: 500px;
height: 100%;
width: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
// Don't forget to replace <YOUR_ACCESS_TOKEN> by your own access token
const accessToken = "<YOUR_ACCESS_TOKEN>";
const map = new maplibregl.Map({
container: "map",
style: `https://api.jawg.io/styles/jawg-sunny.json?access-token=${accessToken}`,
center: [-61.572646, 16.233131],
zoom: 9,
}).addControl(new maplibregl.NavigationControl(), "top-right");
// This plugin is used for right to left languages
maplibregl.setRTLTextPlugin("https://unpkg.com/@mapbox/[email protected]/mapbox-gl-rtl-text.min.js");
// Basic popup definition
new maplibregl.Popup({
closeOnClick: false,
})
.setLngLat([-61.572646, 16.273131])
.setHTML("<b>Hello world!</b><br/> I am a popup.")
.addTo(map);
// Popup definition before binding it to a marker
const markerPopup = new maplibregl.Popup({
closeOnClick: true,
}).setHTML("<b> A popup that is shown when you click on a marker</b>");
// Connect the popup to a new marker
new maplibregl.Marker().setLngLat([-61.653889, 15.966636]).setPopup(markerPopup).addTo(map);
</script>
</body>
</html>