Skip to content

Commit bad10cc

Browse files
Update map to take touch events (#267)
1 parent b243c63 commit bad10cc

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

src/map/index.tsx

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ const MapComponent = ({
189189

190190
const mapRef = useRef<MapRef>(null);
191191
const drawRef = useRef<MaplibreTerradrawControl | null>(null);
192+
const touchStartTimeRef = useRef<number | null>(null);
193+
const touchLocationRef = useRef<{ x: number; y: number } | null>(null);
194+
const handledLongPressRef = useRef<boolean>(false);
192195

193196
// Throttle heightgraph hover updates for better performance
194197
const throttledSetHeightgraphHoverDistance = useMemo(
@@ -633,6 +636,12 @@ const MapComponent = ({
633636
// Handle map click
634637
const handleMapClick = useCallback(
635638
(event: { lngLat: { lng: number; lat: number } }) => {
639+
// Prevent click if we just handled a long press
640+
if (handledLongPressRef.current) {
641+
handledLongPressRef.current = false;
642+
return;
643+
}
644+
636645
// Check if TerraDraw is in an active drawing mode
637646
if (drawRef.current) {
638647
const terraDrawInstance = drawRef.current.getTerraDrawInstance();
@@ -681,6 +690,53 @@ const MapComponent = ({
681690
localStorage.setItem('last_center', last_center);
682691
}, []);
683692

693+
const handleTouchStart = useCallback((event: maplibregl.MapTouchEvent) => {
694+
touchStartTimeRef.current = new Date().getTime();
695+
touchLocationRef.current = { x: event.point.x, y: event.point.y };
696+
handledLongPressRef.current = false;
697+
}, []);
698+
699+
const handleTouchEnd = useCallback(
700+
(event: maplibregl.MapTouchEvent) => {
701+
const longTouchTimeMS = 100;
702+
const acceptableMoveDistance = 20;
703+
704+
if (touchStartTimeRef.current && touchLocationRef.current) {
705+
const touchTime = new Date().getTime() - touchStartTimeRef.current;
706+
const didNotMoveMap =
707+
Math.abs(event.point.x - touchLocationRef.current.x) <
708+
acceptableMoveDistance &&
709+
Math.abs(event.point.y - touchLocationRef.current.y) <
710+
acceptableMoveDistance;
711+
712+
if (touchTime > longTouchTimeMS && didNotMoveMap) {
713+
if (drawRef.current) {
714+
const terraDrawInstance = drawRef.current.getTerraDrawInstance();
715+
if (terraDrawInstance) {
716+
const mode = terraDrawInstance.getMode();
717+
if (
718+
mode === 'polygon' ||
719+
mode === 'select' ||
720+
mode === 'delete-selection'
721+
) {
722+
touchStartTimeRef.current = null;
723+
touchLocationRef.current = null;
724+
return;
725+
}
726+
}
727+
}
728+
729+
handledLongPressRef.current = true;
730+
handleMapContextMenu({ lngLat: event.lngLat });
731+
}
732+
}
733+
734+
touchStartTimeRef.current = null;
735+
touchLocationRef.current = null;
736+
},
737+
[handleMapContextMenu]
738+
);
739+
684740
// Handle route line hover
685741
const onRouteLineHover = useCallback(
686742
(event: maplibregl.MapLayerMouseEvent) => {
@@ -1028,6 +1084,8 @@ const MapComponent = ({
10281084
onMoveEnd={handleMoveEnd}
10291085
onClick={handleMapClick}
10301086
onContextMenu={handleMapContextMenu}
1087+
onTouchStart={handleTouchStart}
1088+
onTouchEnd={handleTouchEnd}
10311089
onMouseMove={handleMouseMove}
10321090
onMouseLeave={handleMouseLeave}
10331091
interactiveLayerIds={['routes-line']}

0 commit comments

Comments
 (0)