From f2ecc1803f3ea294a0c6b7915b61348ed0395b26 Mon Sep 17 00:00:00 2001 From: Marcin Zelent Date: Wed, 16 Nov 2022 15:16:38 +0100 Subject: Remade and extended the app using React --- src/lib/MarkerClusterGroup.js | 32 ++++++++++++++++++++++++++++++++ src/lib/util.ts | 30 ++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 src/lib/MarkerClusterGroup.js create mode 100644 src/lib/util.ts (limited to 'src/lib') diff --git a/src/lib/MarkerClusterGroup.js b/src/lib/MarkerClusterGroup.js new file mode 100644 index 0000000..70e2134 --- /dev/null +++ b/src/lib/MarkerClusterGroup.js @@ -0,0 +1,32 @@ +import L from 'leaflet' +import { createPathComponent } from '@react-leaflet/core' +import 'leaflet.markercluster' + +function createMarkerCluster({ children: _c, ...props }, context) { + const clusterProps = {} + const clusterEvents = {} + // Splitting props and events to different objects + Object.entries(props).forEach(([propName, prop]) => + propName.startsWith('on') + ? (clusterEvents[propName] = prop) + : (clusterProps[propName] = prop) + ) + const instance = new L.MarkerClusterGroup(clusterProps) + + // Initializing event listeners + Object.entries(clusterEvents).forEach(([eventAsProp, callback]) => { + const clusterEvent = `cluster${eventAsProp.substring(2).toLowerCase()}` + instance.on(clusterEvent, callback) + }) + return { + instance, + context: { + ...context, + layerContainer: instance, + }, + } +} + +const MarkerClusterGroup = createPathComponent(createMarkerCluster) + +export default MarkerClusterGroup \ No newline at end of file diff --git a/src/lib/util.ts b/src/lib/util.ts new file mode 100644 index 0000000..1b000c0 --- /dev/null +++ b/src/lib/util.ts @@ -0,0 +1,30 @@ +/** + * Converts time in seconds to HH:mm format. + * @param time Time to convert in seconds. + */ +export function secondsToTimeString(time: number): string { + const h = Math.floor(time / 3600); + const m = Math.floor((time % 3600) / 60); + + return `${h < 10 ? `0${h}` : h}:${m < 10 ? `0${m}` : m}`; +} + +/** + * Calculates distance between two geographical points. + * @param latlng1 Coordinates of the first point. + * @param latlng2 Coordinates of the second point. + */ +export function distanceBetween(latlng1: number[], latlng2: number[]): number { + const R = 6371000; + const rad = Math.PI / 180; + const lat1 = latlng1[0] * rad; + const lat2 = latlng2[0] * rad; + const sinDLat = Math.sin(((latlng2[0] - latlng1[0]) * rad) / 2); + const sinDLon = Math.sin(((latlng2[1] - latlng1[1]) * rad) / 2); + const a = sinDLat * sinDLat + Math.cos(lat1) * Math.cos(lat2) * sinDLon * sinDLon; + const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); + + return R * c; +} + +export default { secondsToTimeString, distanceBetween }; -- cgit v1.2.3