parent
c675cf36ee
commit
221467fa27
@ -0,0 +1,12 @@ |
||||
import { useMemo } from 'preact/hooks'; |
||||
import type { TotalProps } from './types'; |
||||
import { getRouteLength } from '../shared/routes'; |
||||
|
||||
export const TotalComponent = ({ markers }: TotalProps) => { |
||||
const routeLength = useMemo( |
||||
() => getRouteLength(markers.map(({ coordinates }) => coordinates)), |
||||
[markers], |
||||
); |
||||
|
||||
return <>{`Total route length: ${routeLength.toFixed(0)}m`}</>; |
||||
}; |
@ -0,0 +1,16 @@ |
||||
import haversineDistance from 'haversine-distance'; |
||||
import { Coordinates } from './types'; |
||||
|
||||
export const getRouteLength = (points: Coordinates[]) => { |
||||
let result = 0; |
||||
for (let i = 0; i < points.length; i++) { |
||||
const currentPoint = points[i]; |
||||
const previousPoint = points[i - 1]; |
||||
if (!currentPoint || !previousPoint) { |
||||
continue; |
||||
} |
||||
result += haversineDistance(previousPoint, currentPoint); |
||||
} |
||||
|
||||
return result; |
||||
}; |
Loading…
Reference in new issue