Center the map on a clicked symbol
Using events and flyTo to center the map on a symbol.
See the example: https://docs.mapbox.com//mapbox-gl-js/example/center-on-symbol/
HTML
<script src="https://api.mapbox.com/mapbox-gl-js/v1.11.0/mapbox-gl.js"></script>
<link rel="stylesheet" href="https://api.mapbox.com/mapbox-gl-js/v1.11.0/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 = '';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/light-v10',
center: [-90.96, -0.47],
zoom: 8
});
map.on('load', function() {
// Add a GeoJSON source with 3 points.
map.addSource('points', {
'type': 'geojson',
'data': {
'type': 'FeatureCollection',
'features': [
{
'type': 'Feature',
'properties': {},
'geometry': {
'type': 'Point',
'coordinates': [
-91.395263671875,
-0.9145729757782163
]
}
},
{
'type': 'Feature',
'properties': {},
'geometry': {
'type': 'Point',
'coordinates': [
-90.32958984375,
-0.6344474832838974
]
}
},
{
'type': 'Feature',
'properties': {},
'geometry': {
'type': 'Point',
'coordinates': [
-91.34033203125,
0.01647949196029245
]
}
}
]
}
});
// Add a symbol layer.
map.addLayer({
'id': 'symbols',
'type': 'symbol',
'source': 'points',
'layout': {
'icon-image': 'marker-15'
}
});
// Center the map on the...