Last active
July 6, 2021 23:35
-
-
Save sadewole/4af64588a1432845c8ca56a849435e9b to your computer and use it in GitHub Desktop.
Re-center map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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