old-examples
old-examples copied to clipboard
Dynamically Set the center of the map
is this able to set the center of the map?
Action: Clicks a button
Expected output: Center the map, on e.g Egypt.
Year have passed, but i'm gonna answer anyway. Just set map reference on init, and then refer to it and use just native google maps api according to their docs, here's simplified example:
import { useEffect, useRef } from 'preact/hooks';
import GoogleMapReact from 'google-map-react';
const CustomMap = () => {
const mapRef = useRef(null);
useEffect(() => {
mapRef.current &&
mapRef.current.setCenter({ lat: 10, lng: 20 });
}, []);
return (
<div>
<GoogleMapReact
yesIWantToUseGoogleMapApiInternals
onGoogleApiLoaded={({ map }) => {
mapRef.current = map;
}}
>
</GoogleMapReact>
</div>
);
};
export default CustomMap;