|
@@ -1,8 +1,10 @@
|
|
import React, { useRef, useEffect } from "react";
|
|
import React, { useRef, useEffect } from "react";
|
|
/* global google */
|
|
/* global google */
|
|
|
|
|
|
-const MapComponent = React.memo(({
|
|
|
|
- onMapClick
|
|
|
|
|
|
+// avoid re-rendering with memo(..., () => true)
|
|
|
|
+// this reduces reusability of this component, but that's fine for this
|
|
|
|
+export default React.memo(({
|
|
|
|
+ onMarkerMoved
|
|
}) => {
|
|
}) => {
|
|
const mapDivRef = useRef(null);
|
|
const mapDivRef = useRef(null);
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
@@ -12,10 +14,17 @@ const MapComponent = React.memo(({
|
|
disableDefaultUI: true,
|
|
disableDefaultUI: true,
|
|
fullscreenControl: true,
|
|
fullscreenControl: true,
|
|
});
|
|
});
|
|
- console.log("Created Map");
|
|
|
|
- if (onMapClick) {
|
|
|
|
- console.log("Adding click listener");
|
|
|
|
- googleMap.addListener("click", ({latLng}) => onMapClick(googleMap, latLng));
|
|
|
|
|
|
+
|
|
|
|
+ if (onMarkerMoved) {
|
|
|
|
+ googleMap.addListener("click", ({ latLng }) => {
|
|
|
|
+ if (mapDivRef.current.mapMarker) {
|
|
|
|
+ mapDivRef.current.mapMarker.setMap(null);
|
|
|
|
+ }
|
|
|
|
+ mapDivRef.current.mapMarker = new google.maps.Marker({ map: googleMap, position: latLng });
|
|
|
|
+ if (onMarkerMoved) {
|
|
|
|
+ onMarkerMoved({ lat: latLng.lat(), lng: latLng.lng() });
|
|
|
|
+ }
|
|
|
|
+ });
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
|
|
@@ -26,19 +35,3 @@ const MapComponent = React.memo(({
|
|
/>
|
|
/>
|
|
);
|
|
);
|
|
}, () => true);
|
|
}, () => true);
|
|
-
|
|
|
|
-export default ({
|
|
|
|
- onMarkerMoved,
|
|
|
|
-}) => {
|
|
|
|
- // avoid re-rendering by using let (similar to instance field)
|
|
|
|
- let marker = null;
|
|
|
|
- return <MapComponent onMapClick={(map, position) => {
|
|
|
|
- if (marker) {
|
|
|
|
- marker.setMap(null);
|
|
|
|
- }
|
|
|
|
- marker = new google.maps.Marker({ map, position });
|
|
|
|
- if (onMarkerMoved) {
|
|
|
|
- onMarkerMoved({ lat: position.lat(), lng: position.lng() });
|
|
|
|
- }
|
|
|
|
- }}/>
|
|
|
|
-};
|
|
|