Search code examples
reactjsgoogle-mapsreact-google-maps

How to move the map by code in react-google-maps?


enter image description here

Hi 👋 i want to make a button that can move the map to the user current location, i read the docs but i still don't understand which code to move the map like when we type and enter a location in autocomplete, or should i re-render the map with new lat long?


Solution

  • Actually, you have to change the center of the map to the user location and re-render the map component. Like this:

    <GoogleMap
        id="marker-example"
        mapContainerStyle={mapContainerStyle}
        zoom={2}
        center={center}
      >
        <Marker
          onLoad={onLoad}
          position={position}
        />
      </GoogleMap>
    

    Have you tried this?