Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically Set the center of the map #42

Open
DevChrisDev opened this issue Mar 21, 2020 · 1 comment
Open

Dynamically Set the center of the map #42

DevChrisDev opened this issue Mar 21, 2020 · 1 comment

Comments

@DevChrisDev
Copy link

is this able to set the center of the map?

Action:
Clicks a button

Expected output:
Center the map, on e.g Egypt.

@maciejkwas
Copy link

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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants