I have react app using react-google-maps. I want it to have marker fixed at the center of map when user moves the map. But in my case, whenever dragging event happens map is just immediately refreshed.
parent component:
import React, { useState, useEffect } from 'react';
import Map from './Map';
export default function Parent() {
const [mapCenter, setMapCenter] = useState(undefined);
const [markerPosition, setMarkerPosition] = useState(undefined);
useEffect(()=>navigator.geolocation.getCurrentPosition(position =>
{
setMapCenter({lat:position.coords.latitude, lng:position.coords.longitude})
setMarkerPosition({lat:position.coords.latitude, lng:position.coords.longitude})
}
),[])
return(
<div>
{ mapCenter && <Map mapCenter={mapCenter} setMapCenter={setMapCenter} markerPosition={markerPosition} setMarkerPosition={setMarkerPosition} />}
</div>
)
}
and the map component:
import React from 'react';
import { GoogleMap, Marker, withGoogleMap } from "react-google-maps";
export default function Map(props) {
let mapRef;
let markerRef;
const WrappedMap = withGoogleMap(() =>
<GoogleMap ref={(ref) => mapRef = ref} center={props.mapCenter} zoom={16} onDrag={()=>{
const newCenter = mapRef.getCenter().toJSON();
props.setMarkerPosition(newCenter);
props.setMapCenter(newCenter)
}}>
<Marker ref={(ref) => markerRef = ref } position={props.markerPosition} draggable={false}/>
</GoogleMap>
);
return (
<div style={{ height: "50vh", width: "50vh" }}>
<WrappedMap
loadingElement={<div style={{ height: "100%" }} />}
containerElement={<div id="map" style={{ height: "100%" }} />}
mapElement={<div style={{ height: "100%" }} />}
/>
</div>
)
}
I expected that when user drag map, parent's states get updated and marker moves to the new location but it doesn't work as I expected. What am I missing here?