I am working with GoogleMap
component from @react-google-maps/api
, but can't seem to find how can I get the currently centered map coordinates (lat & lng) after I have moved it?
By searching around I found this article, which asks a similar question, but none of the answers work for me. Below is a code I'm testing.
import { GoogleMap, useLoadScript } from "@react-google-maps/api";
const { isLoaded, loadError } = useLoadScript({
googleMapsApiKey: process.env.REACT_APP_GOOGLE_MAPS_API_KEY,
});
export default function Test() {
return (
<>
<GoogleMap
zoom={8}
center={{lat: 35.6676095, lng: 139.334863}}
// onCenterChanged={getCenter} - doesn't work
// onCenterChanged={getCenter()} - doesn't work
// onCenterChanged={this.getCenter} - doesn't work
// onCenterChanged={ (e)=> this.getCenter(e)} - doesn't work
>
</GoogleMap>
</>
);
}
The map loads fine, but once I add the onCenterChanged=
prop, everything breaks because the getCenter
function is obviously not declared.
I'd like to get a variable or state with the center coordinates after I have moved the map. Where do I declare it and how do I use it?