react-google-maps-api
react-google-maps-api copied to clipboard
How to re-center when clicking on a marker?
Hello, I am unable to do this, it's probably simple but I can't figure it out.
After the map is loaded, with markers, when I click on a marker I would like to re-center the map to the position of that marker. How can I do that?
Thanks.
You want to have a state to store your map reference:
const [map, setMap] = useState()
Then on your onLoad in the map:
<GoogleMap
.....
onLoad={(map) => setMap(map)} />
Then in your marker:
<Marker
position={position}
onClick={() => map.setCenter(position)} />
Here's a quick example of how to do it: