|
| 1 | +import MapLibreGL, { LineLayerStyle } from "@maplibre/maplibre-react-native"; |
| 2 | +import { Feature, LineString, Point } from "geojson"; |
| 3 | +import React, { useEffect, useState } from "react"; |
| 4 | + |
| 5 | +import sheet from "../../styles/sheet"; |
| 6 | +import RouteSimulator from "../../utils/RouteSimulator"; |
| 7 | +import Page from "../common/Page"; |
| 8 | +import PulseCircleLayer from "../common/PulseCircleLayer"; |
| 9 | + |
| 10 | +const ROUTE_FEATURE: Feature<LineString> = { |
| 11 | + type: "Feature", |
| 12 | + geometry: { |
| 13 | + type: "LineString", |
| 14 | + coordinates: [ |
| 15 | + [8.070566386917108, 53.295365558646694], |
| 16 | + [13.37432488261203, 52.50503393836857], |
| 17 | + [7.218077210609579, 50.98270036522064], |
| 18 | + [13.652081261390094, 49.583747114745165], |
| 19 | + [8.1694637345079, 47.9516814815924], |
| 20 | + ], |
| 21 | + }, |
| 22 | + properties: {}, |
| 23 | +}; |
| 24 | + |
| 25 | +const layerStyles: { |
| 26 | + route: LineLayerStyle; |
| 27 | + progress: LineLayerStyle; |
| 28 | +} = { |
| 29 | + route: { |
| 30 | + lineColor: "white", |
| 31 | + lineCap: "round", |
| 32 | + lineWidth: 3, |
| 33 | + lineOpacity: 0.84, |
| 34 | + }, |
| 35 | + progress: { |
| 36 | + lineColor: "#314ccd", |
| 37 | + lineWidth: 3, |
| 38 | + }, |
| 39 | +}; |
| 40 | + |
| 41 | +export default function AnimateCircleAlongLine() { |
| 42 | + const [currentPoint, setCurrentPoint] = |
| 43 | + useState<Feature<Point, { distance: number; nearestIndex: number }>>(); |
| 44 | + |
| 45 | + useEffect(() => { |
| 46 | + const routeSimulator = new RouteSimulator(ROUTE_FEATURE); |
| 47 | + |
| 48 | + routeSimulator.addListener( |
| 49 | + (point: Feature<Point, { distance: number; nearestIndex: number }>) => { |
| 50 | + setCurrentPoint(point); |
| 51 | + }, |
| 52 | + ); |
| 53 | + |
| 54 | + routeSimulator.start(); |
| 55 | + |
| 56 | + return () => { |
| 57 | + routeSimulator.stop(); |
| 58 | + }; |
| 59 | + }, []); |
| 60 | + |
| 61 | + const renderProgressLine = () => { |
| 62 | + if (!currentPoint) { |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + const { nearestIndex } = currentPoint.properties; |
| 67 | + const coords = ROUTE_FEATURE.geometry.coordinates.filter( |
| 68 | + (c, i) => i <= nearestIndex, |
| 69 | + ); |
| 70 | + coords.push(currentPoint.geometry.coordinates); |
| 71 | + |
| 72 | + if (coords.length < 2) { |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + const lineString: LineString = { type: "LineString", coordinates: coords }; |
| 77 | + |
| 78 | + return ( |
| 79 | + <MapLibreGL.Animated.ShapeSource id="progressSource" shape={lineString}> |
| 80 | + {/* @ts-ignore */} |
| 81 | + <MapLibreGL.Animated.LineLayer |
| 82 | + id="progress-line" |
| 83 | + style={layerStyles.progress} |
| 84 | + aboveLayerID="route-line" |
| 85 | + /> |
| 86 | + </MapLibreGL.Animated.ShapeSource> |
| 87 | + ); |
| 88 | + }; |
| 89 | + |
| 90 | + return ( |
| 91 | + <Page> |
| 92 | + <MapLibreGL.MapView style={sheet.matchParent}> |
| 93 | + <MapLibreGL.Camera |
| 94 | + defaultSettings={{ |
| 95 | + bounds: { |
| 96 | + ne: [-3.419709, 44.452929], |
| 97 | + sw: [25.309539, 55.766941], |
| 98 | + }, |
| 99 | + }} |
| 100 | + /> |
| 101 | + |
| 102 | + <MapLibreGL.ShapeSource id="route-source" shape={ROUTE_FEATURE}> |
| 103 | + <MapLibreGL.LineLayer id="route-line" style={layerStyles.route} /> |
| 104 | + </MapLibreGL.ShapeSource> |
| 105 | + |
| 106 | + {currentPoint && <PulseCircleLayer shape={currentPoint} />} |
| 107 | + |
| 108 | + {renderProgressLine()} |
| 109 | + </MapLibreGL.MapView> |
| 110 | + </Page> |
| 111 | + ); |
| 112 | +} |
0 commit comments