All docsMapbox GL JSExamplesShow polygon information on click

Show polygon information on click

In this example, when a user clicks a polygon in a specified layer, the map shows a popup containing information about the clicked feature.

When the map loads, it uses addSource and addLayer to add and style a layer called states-layer, which contains state polygons.

When the user clicks a feature in the states-layer layer, the example uses the Popup method to create a new popup, using MapMouseEvent data to set the position and content of the popup.

Lastly, it uses the Map events mouseenter and mouseleave to toggle the user's cursor to a pointer while it hovers over any feature in states-layer.

<!DOCTYPE html>
<meta charset="utf-8">
<title>Show polygon information on click</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<link href="" rel="stylesheet">
<script src=""></script>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
.mapboxgl-popup {
max-width: 400px;
font: 12px/20px 'Helvetica Neue', Arial, Helvetica, sans-serif;
<div id="map"></div>
mapboxgl.accessToken = 'YOUR_MAPBOX_ACCESS_TOKEN';
// Create a new map.
const map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11',
center: [-100.04, 38.907],
zoom: 3
map.on('load', () => {
// Add a source for the state polygons.
map.addSource('states', {
'type': 'geojson',
'data': ''
// Add a layer showing the state polygons.
'id': 'states-layer',
'type': 'fill',
'source': 'states',
'paint': {
'fill-color': 'rgba(200, 100, 240, 0.4)',
'fill-outline-color': 'rgba(200, 100, 240, 1)'
// When a click event occurs on a feature in the states layer,
// open a popup at the location of the click, with description
// HTML from the click event's properties.
map.on('click', 'states-layer', (e) => {
new mapboxgl.Popup()
// Change the cursor to a pointer when
// the mouse is over the states layer.
map.on('mouseenter', 'states-layer', () => {
map.getCanvas().style.cursor = 'pointer';
// Change the cursor back to a pointer
// when it leaves the states layer.
map.on('mouseleave', 'states-layer', () => {
map.getCanvas().style.cursor = '';