How do I keep the marker centered of the map, while dragging the map around the marker? Like how Uber does it, when a user is trying to choose a location. Any ideas?
@asonni You need to alter the center prop of your map whenever the position changes
<GoogleMap defaultZoom={18} center={{lat: props.latitude || 0, lng: props.longitude || 0}} defaultCenter={{ lat: 0, lon: 0 }}>
</GoogleMap>
@neilyoung Thanks for the idea
I found this function in GoogleMap prop onBoundsChanged
const onBoundsChanged = () => {
this.setState({
bounds: refs.map.getBounds(),
center: refs.map.getCenter()
});
}
And then I changed Marker position props
<Marker
position={this.state.center}
/>
@asonni Ah, ok. You come from the other side, you change bounds or pan the map and you want to keep you maker always centered. In that scenario your solution works better. In my case I'm moving the marker and keep the map centered around. However, I guess, both solutions could coexist.
I just overlay the map with a div and and icon positioned at the center.
GitHub issue tracker is NOT designed for how to questions
@asonni how to get refs.map?
Most helpful comment
@neilyoung Thanks for the idea
I found this function in GoogleMap prop onBoundsChanged
const onBoundsChanged = () => { this.setState({ bounds: refs.map.getBounds(), center: refs.map.getCenter() }); }And then I changed Marker position props
<Marker position={this.state.center} />