Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Google Maps Javascript Api cant take coordinates from useState

I am trying to render google maps using javascipt api. It gets correctly rendered when it is statically typed like

const coords = { lat:0,lng:0 };

but when i use usestates to set coordinates , it shows an warning message that GoogleMap: center or defaultcenter property must be defined.

<GoogleMapReact
        
        bootstrapURLKeys={{ key: 'xxx' }}
        defaultCenter={coordinates}
        center={coordinates}
        defaultZoom={14}
        margin={[50, 50, 50, 50]}
        options={''}
        onChange={(e)=>{
          console.log(e);
          setCoordinates({lat:e.center.lat,lng:e.center.lng});
        }}
        onChildClick={''}
        
      >

Here is the useStates function

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const [coordinates, setCoordinates] = useState({});

Here is my Map function

<Map 
     setCoordinates={setCoordinates}
                
     setBounds={setBounds}
     coordinates={coordinates}
            
            />

>Solution :

You have to define the values

const [coordinates, setCoordinates] = useState({lat: 0, lng: 0 });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading