Skip to content

How can I get mapInstance properly ? #2458

Answered by ghost
ghost asked this question in Q&A
Discussion options

You must be logged in to vote

Hello guys,
As I said, this was a mistake I made using React, it's not related to this library. I solved it with this code:

/* global google */
export default function App() {
  const mapRef = useRef<google.maps.Map>();

  const onLoad = (map: any) => {
    mapRef.current = map;
  };

  const onBoundsChanged = () => {
    if (mapRef.current) {
      const bounds = mapRef.current.getBounds();
      console.log('onBoundsChange', bounds);
    }
  };

  return (
    <LoadScript
      googleMapsApiKey={API_KEY}
    >
      <GoogleMap
        onLoad={onLoad}
        onBoundsChanged={onBoundsChanged}
        center={center}
        zoom={12}
        mapContainerStyle={containerStyle}
      >

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
0 participants