Skip to content

Instantly share code, notes, and snippets.

@sadewole
Last active July 6, 2021 23:35
Show Gist options
  • Save sadewole/4af64588a1432845c8ca56a849435e9b to your computer and use it in GitHub Desktop.
Save sadewole/4af64588a1432845c8ca56a849435e9b to your computer and use it in GitHub Desktop.
Re-center map
useEffect(() => {
if (!map.current) return; // Waits for the map to initialise
const results = fetchData();
results.then((marker) => {
// create a HTML element for each feature
var el = document.createElement('div');
el.className = 'marker';
// make a marker for each feature and add it to the map
new mapboxgl.Marker(el)
.setLngLat(marker.geometry.coordinates)
.setPopup(
new mapboxgl.Popup({ offset: 25 }) // add popups
.setHTML('<p>' + marker.properties.description + '</p>')
)
.addTo(map.current);
map.current.on('load', async () => {
map.current.flyTo({
center: marker.center,
});
});
});
}, [fetchData]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment