Attach a popup to a marker instance
Attach a popup to a marker and display it on click.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Attach a popup to a marker instance</title>
<meta property="og:description" content="Attach a popup to a marker and display it on click." />
<meta charset='utf-8'>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/trackasia-gl.css" />
<script src="https://unpkg.com/[email protected]/dist/trackasia-gl.js"></script>
<style>
body { margin: 0; padding: 0; }
html, body, #map { height: 100%; }
</style>
</head>
<body>
<style>
#marker {
background-image: url('https://docs.track-asia.com/assets/Opera_Ho_Chi_Minh.jpeg');
background-size: cover;
width: 50px;
height: 50px;
border-radius: 50%;
cursor: pointer;
}
.trackasiagl-popup {
max-width: 200px;
}
</style>
<div id="map"></div>
<script>
const opera = [106.70327,10.776694];
const map = new trackasiagl.Map({
container: 'map',
style: 'https://maps.track-asia.com/styles/v2/streets.json?key=public_key',
center: opera,
zoom: 16
});
// create the popup
const popup = new trackasiagl.Popup({ offset: 25 }).setText(
'The Opera Ho Chi Minh City, by Diego Delso, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=30088100'
);
// create DOM element for the marker
const el = document.createElement('div');
el.id = 'marker';
// create the marker
new trackasiagl.Marker({element: el})
.setLngLat(opera)
.setPopup(popup) // sets a popup on this marker
.addTo(map);
</script>
</body>
</html>