Google-maps-react: How can i set a marker always at the map's center?

Created on 26 Jan 2018  路  2Comments  路  Source: fullstackreact/google-maps-react

I want to put a marker at the center of the map and make it stay there after dragging, as the marker did not move at all, like Uber does. This is my code.

export class MapContainer extends Component {
render() {
    return (
      <Map
        google={this.props.google}
        mapTypeControl={false}
        zoomControl={false}
        gestureHandling="greedy"
        zoom={15}
        center={this.props.center}
        onDragend={this.props.onCenterChanged}
      >
        <Marker name={'Current location'} position={this.props.center} />
      </Map>
    );
  }
}

And the function in a parent component

handleCenterChanged(mapProps, map) {
    console.log(map);
    const newCenter = {lat: map.center.lat(), lng: map.center.lng()};
    const newMarker = update(this.state.markers, {
      position: {
        lat: { $set: newCenter.lat },
        lng: { $set: newCenter.lng },
      },
    });
    this.setState({ markers: newMarker, center: newMarker.position });
  }

With this, the marker moves to the center so bad. It seems with a big delay. If anyone could help me i麓ll be grateful :)

Most helpful comment

I'm cloning uber as well and the way I solved this wasn't putting a marker on the center, I actually render the map component and an image component separately on the center of the screen and when people drag the map I just call map.getCenter() which will give you the address where the centered image is.

All 2 comments

Did you try to use initialCenter in the map component?

I'm cloning uber as well and the way I solved this wasn't putting a marker on the center, I actually render the map component and an image component separately on the center of the screen and when people drag the map I just call map.getCenter() which will give you the address where the centered image is.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cernoel picture cernoel  路  3Comments

rullymartanto picture rullymartanto  路  5Comments

t1a2l picture t1a2l  路  5Comments

carmen79 picture carmen79  路  4Comments

kevinSuttle picture kevinSuttle  路  4Comments