Get features under the mouse pointer
Use queryRenderedFeatures to show properties of hovered-over map elements.
See the example: https://docs.mapbox.com//mapbox-gl-js/example/queryrenderedfeatures/
by Tristen Brown
December 16, 2024
HTML
<script src="https://api.mapbox.com/mapbox-gl-js/v3.9.0-beta.1/mapbox-gl.js"></script>
<link rel="stylesheet" href="https://api.mapbox.com/mapbox-gl-js/v3.9.0-beta.1/mapbox-gl.css">
<div id="map"></div>
CSS
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
JavaScript
mapboxgl.accessToken =
"pk.eyJ1IjoidHJpc3RlbiIsImEiOiJjajZoOXU4Z2kwNnppMnlxd2d6bTFvZ2xtIn0.PP7AoUakMfeqdXFHdsY0oA"
const style = {
version: 8,
name: "Blank",
imports: [
{
id: "basemap",
url: "mapbox://styles/tristen/cm4r8quxh00ct01ryevcicrpm",
config: { theme: "monochrome" },
},
],
sources: {},
glyphs: "mapbox://fonts/mapbox/{fontstack}/{range}.pbf",
projection: { name: "globe" },
layers: []
}
const map = new mapboxgl.Map({
container: "map",
style,
center: [-79.38048, 43.64929],
zoom: 14
})