1
votes

I'm rendering a GoogleMap component using the "react-google-maps" library in React. I can set an initial defaultLocation that works fine when the map initially loads. In the documentation it says that the component takes a "center" prop that one can pass a lat and a lng to but I can't get it to re-render/re-center to a new location. It always shows the default location. Any idea what I am doing wrong?

Is there maybe a way how I can use state outside of the addAdressComponent class so I can dynamically set the initial defaultCenter instead of using props in the render section?

import { withGoogleMap, GoogleMap, Marker } from 'react-google-maps';
/*global google*/

const MapWithMarker = withGoogleMap((props) => (
  <GoogleMap
    defaultZoom={12}
    defaultCenter={{ lat: 47.376888, lng: 8.541694 }}
    options={{
      disableDefaultUI: true,
    }}
  >
    <Marker position={{ lat: 47.376888, lng: 8.541694 }} />
  </GoogleMap>
));

class addAddressComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      lat: 0,
      lng: 0,
    };
    this.onSuggestSelect = this.onSuggestSelect.bind(this);
  }

  onSuggestSelect(suggest) {
    console.log(suggest.location);
    this.setState(() => {
      return {
        lat: 10.0,
        lng: 20.022,
      };
    });
  }
  render() {
    return (
      <div className="wrapperClass">
        <MapWithMarker
          containerElement={<div style={{ height: '244px' }} />}
          mapElement={<div style={{ height: '100%' }} />}
          className="mapCSS"
          center={(this.state.lat, this.state.lng)}
          style={{
            width: '348px',
            height: '250px',
          }}
        />
      </div>
    );
  }
}

export default addAddressComponent;
1

1 Answers

0
votes

You'll need to pass the center property to your GoogleMap component inside your MapWithMarker component. Also the center argument needs to be an object like this:

{ lat: -34.397, lng: 150.644 }

The following example lets you change the center of the map using the button:

import logo from './logo.svg';
import './App.css';
import { withScriptjs, withGoogleMap, GoogleMap, Marker } from 'react-google-maps';
import {useState} from "react";
const MyMapComponent = withScriptjs(withGoogleMap((props) =>
    <GoogleMap
        defaultZoom={13}
        center={props.center}
        defaultCenter={{ lat: -34.397, lng: 150.644 }}
    >
      {props.isMarkerShown && <Marker position={{ lat: -34.397, lng: 150.644 }} />}
    </GoogleMap>
))
function App() {
  const [position, setPosition] = useState({ lat: -34.397, lng: 150.644 });
  return (
    <div className="App">
      <button onClick={() => setPosition({lat: 30, lng: 10})}>,
        Click me
      </button>
      <MyMapComponent
          isMarkerShown
          googleMapURL="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=geometry,drawing,places"
          loadingElement={<div style={{ height: `100%` }} />}
          containerElement={<div style={{ height: `100%` }} />}
          center={position}
          mapElement={<div style={{ height: `1000px`}} />}
      />
    </div>
  );
}
export default App;