Skip to content

Commit

Permalink
map marker: show updated location
Browse files Browse the repository at this point in the history
  • Loading branch information
SteRiccio committed Sep 15, 2023
1 parent 651c813 commit 6a8c4e5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
9 changes: 6 additions & 3 deletions webapp/components/Map/MapMarker/MapMarker.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import { useMapMarker } from './useMapMarker'
export const MapMarker = (props) => {
const { editable, point, title } = props

const { markerEventHandlers, markerRef, pointLatLon } = useMapMarker(props)
const { markerEventHandlers, markerRef, pointLatLon, pointUpdated } = useMapMarker(props)

if (!pointLatLon) return null
if (!pointLatLon) {
// invalid location
return null
}

return (
<Marker draggable={editable} eventHandlers={markerEventHandlers} position={pointLatLon} ref={markerRef}>
<MapMarkerPopup point={point} title={title} />
<MapMarkerPopup point={pointUpdated ?? point} title={title} />
</Marker>
)
}
28 changes: 15 additions & 13 deletions webapp/components/Map/MapMarker/useMapMarker.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ export const useMapMarker = (props) => {

const [state, setState] = useState({
pointLatLon: null, // array with latitude and longitude values
pointUpdated: null,
})

const { pointLatLon } = state
const { pointLatLon, pointUpdated } = state
const pointSrs = point?.srs

const fromPointToLatLon = useCallback(
Expand All @@ -43,17 +44,18 @@ export const useMapMarker = (props) => {

const onPointUpdated = useCallback(
({ lat, lng }) => {
setState((statePrev) => ({
...statePrev,
pointLatLon: [lat, lng],
}))

let pointUpdated = PointFactory.createInstance({ x: lng, y: lat })

// transform updated location into a location with the same SRS as the marker position parameter
if (pointSrs && pointSrs !== pointUpdated.srs) {
pointUpdated = Points.transform(pointUpdated, pointSrs, srsIndex)
}
setState((statePrev) => ({
...statePrev,
pointLatLon: [lat, lng],
pointUpdated,
}))

onPointUpdatedProp(pointUpdated)
},
[onPointUpdatedProp, pointSrs, srsIndex]
Expand All @@ -72,14 +74,14 @@ export const useMapMarker = (props) => {
[onPointUpdated]
)

if (editable) {
useMapEvents({
dblclick(event) {
useMapEvents({
dblclick(event) {
if (editable) {
const { lat, lng } = event.latlng
onPointUpdated({ lat, lng })
},
})
}
}
},
})

return { markerEventHandlers, markerRef, pointLatLon }
return { markerEventHandlers, markerRef, pointLatLon, pointUpdated }
}

0 comments on commit 6a8c4e5

Please sign in to comment.