From 6ee47499de47b5d3c6e7ae57d775a885d547bed4 Mon Sep 17 00:00:00 2001 From: "Kim, Allen" Date: Mon, 17 Feb 2025 14:50:05 -0500 Subject: [PATCH] Enable marker to move around using --- src/lib/interactions/PointerInteraction.tsx | 34 ++++++++++++++++++ src/stories/Marker/Marker.mdx | 1 + src/stories/Marker/Marker.stories.tsx | 3 +- .../PointerInteraction/PointerInteraction.mdx | 35 +------------------ .../PointerInteraction.stories.tsx | 35 +------------------ 5 files changed, 39 insertions(+), 69 deletions(-) diff --git a/src/lib/interactions/PointerInteraction.tsx b/src/lib/interactions/PointerInteraction.tsx index cba9b5e..1c5c973 100644 --- a/src/lib/interactions/PointerInteraction.tsx +++ b/src/lib/interactions/PointerInteraction.tsx @@ -1,12 +1,46 @@ import OlPointerInteraction from 'ol/interaction/Pointer'; import { useEffect } from 'react'; +import { Coordinate } from 'ol/coordinate'; +import { FeatureLike } from 'ol/Feature'; +import MapBrowserEvent from 'ol/MapBrowserEvent'; +import { Geometry } from 'ol/geom' import { useMap } from '../Map'; export function PointerInteraction(props) { const map = useMap(); + let coordinate: Coordinate; + let feature: FeatureLike; + + function handleDownEvent(evt: MapBrowserEvent) { + const _feature = evt.map.forEachFeatureAtPixel(evt.pixel, feature => feature); + _feature && ([coordinate, feature] = [evt.coordinate, _feature]); + return !!feature; // `true` to start the drag sequence. + } + + function handleDragEvent(evt: MapBrowserEvent) { + const geometry = feature.getGeometry() as Geometry; + const deltaX = evt.coordinate[0] - coordinate[0]; + const deltaY = evt.coordinate[1] - coordinate[1]; + geometry.translate(deltaX, deltaY); + coordinate = evt.coordinate; + } + + function handleMoveEvent(evt: MapBrowserEvent) { + const feature = evt.map.forEachFeatureAtPixel(evt.pixel, feature => feature); + const element = evt.map.getTargetElement(); + element.style.cursor = feature ? 'pointer' : ''; + } + + function handleUpEvent() { + coordinate = null; + feature = null; + return false; // return `false` to stop the drag sequence + } + useEffect(() => { if (!map) return; + props = {...{handleDownEvent, handleMoveEvent, handleUpEvent, handleDragEvent}, ...props }; const interaction = new OlPointerInteraction(props); map.addInteraction(interaction); }, [map]); diff --git a/src/stories/Marker/Marker.mdx b/src/stories/Marker/Marker.mdx index b219282..c343951 100644 --- a/src/stories/Marker/Marker.mdx +++ b/src/stories/Marker/Marker.mdx @@ -26,6 +26,7 @@ https://openlayers.org/en/latest/apidoc/module-ol_Feature-Feature.html addOnClick={true} removeOnClick={true} /> + {/* Enable marker move */} ); } diff --git a/src/stories/Marker/Marker.stories.tsx b/src/stories/Marker/Marker.stories.tsx index 577706e..3efb977 100644 --- a/src/stories/Marker/Marker.stories.tsx +++ b/src/stories/Marker/Marker.stories.tsx @@ -1,7 +1,7 @@ import { OSM } from 'ol/source'; import { useRef } from 'react'; import type { Meta, StoryObj } from '@storybook/react'; -import { Map, TileLayer, View, Marker } from '../../lib'; +import { Map, TileLayer, View, Marker, PointerInteraction } from '../../lib'; export default { title: 'Marker' } as Meta; declare let window:any; @@ -15,6 +15,7 @@ export const Primary: StoryObj = { + {/* Enable marker move */} ); } diff --git a/src/stories/interactions/PointerInteraction/PointerInteraction.mdx b/src/stories/interactions/PointerInteraction/PointerInteraction.mdx index 6689852..fdb3e2c 100644 --- a/src/stories/interactions/PointerInteraction/PointerInteraction.mdx +++ b/src/stories/interactions/PointerInteraction/PointerInteraction.mdx @@ -51,45 +51,12 @@ export default function(props) { 'fill-color': [0, 0, 255, 0.5], } - let coordinate: Coordinate; - let feature: FeatureLike; - - function handleDownEvent(evt: MapBrowserEvent) { - const _feature = evt.map.forEachFeatureAtPixel(evt.pixel, feature => feature); - _feature && ([coordinate, feature] = [evt.coordinate, _feature]); - return !!feature; // `true` to start the drag sequence. - } - - function handleDragEvent(evt: MapBrowserEvent) { - const geometry = feature.getGeometry() as Geometry; - const deltaX = evt.coordinate[0] - coordinate[0]; - const deltaY = evt.coordinate[1] - coordinate[1]; - geometry.translate(deltaX, deltaY); - coordinate = evt.coordinate; - } - - function handleMoveEvent(evt: MapBrowserEvent) { - const feature = evt.map.forEachFeatureAtPixel(evt.pixel, feature => feature); - const element = evt.map.getTargetElement(); - element.style.cursor = feature ? 'pointer' : ''; - } - - function handleUpEvent() { - coordinate = null; - feature = null; - return false; // return `false` to stop the drag sequence - } - return ( - + ); } diff --git a/src/stories/interactions/PointerInteraction/PointerInteraction.stories.tsx b/src/stories/interactions/PointerInteraction/PointerInteraction.stories.tsx index f800669..c22d84a 100644 --- a/src/stories/interactions/PointerInteraction/PointerInteraction.stories.tsx +++ b/src/stories/interactions/PointerInteraction/PointerInteraction.stories.tsx @@ -36,45 +36,12 @@ export const Primary = { 'fill-color': [0, 0, 255, 0.5], } - let coordinate: Coordinate; - let feature: FeatureLike; - - function handleDownEvent(evt: MapBrowserEvent) { - const _feature = evt.map.forEachFeatureAtPixel(evt.pixel, feature => feature); - _feature && ([coordinate, feature] = [evt.coordinate, _feature]); - return !!feature; // `true` to start the drag sequence. - } - - function handleDragEvent(evt: MapBrowserEvent) { - const geometry = feature.getGeometry() as Geometry; - const deltaX = evt.coordinate[0] - coordinate[0]; - const deltaY = evt.coordinate[1] - coordinate[1]; - geometry.translate(deltaX, deltaY); - coordinate = evt.coordinate; - } - - function handleMoveEvent(evt: MapBrowserEvent) { - const feature = evt.map.forEachFeatureAtPixel(evt.pixel, feature => feature); - const element = evt.map.getTargetElement(); - element.style.cursor = feature ? 'pointer' : ''; - } - - function handleUpEvent() { - coordinate = null; - feature = null; - return false; // return `false` to stop the drag sequence - } - return ( - + ); }