Projekt

Obecné

Profil

Stáhnout (1.58 KB) Statistiky
| Větev: | Tag: | Revize:
1
import L, { LatLngTuple, Marker as MarkerPOJO } from 'leaflet'
2
import { FunctionComponent, ReactNode, useMemo, useRef, useState } from 'react'
3
import { Marker } from 'react-leaflet'
4
import {
5
    getMapPointIcon,
6
    MapPoint,
7
} from '../trackingToolUtils'
8

    
9
export interface MapMarkerProps {
10
    position: LatLngTuple
11
    children?: ReactNode
12
    mapPoint: MapPoint
13
    color?: 'external' | 'disabled' | 'localCatalog'
14
    updatePositionCallbackFn: (position: LatLngTuple) => void // Callback function to notify MapPath to rerender the path
15
}
16

    
17
// Custom Map Marker component
18
const MapMarker: FunctionComponent<MapMarkerProps> = ({
19
    position,
20
    children,
21
    updatePositionCallbackFn,
22
    mapPoint,
23
}) => {
24
    const [currentPosition, setCurrentPosition] = useState(position)
25
    const markerRef = useRef<MarkerPOJO | null>(null)
26
    const eventHandlers = useMemo(
27
        () => ({
28
            dragend: () => {
29
                const marker = markerRef.current
30
                console.log(markerRef)
31
                if (!marker) {
32
                    return
33
                }
34
                const latlng = marker.getLatLng()
35
                setCurrentPosition([latlng.lat, latlng.lng])
36
                updatePositionCallbackFn([latlng.lat, latlng.lng])
37
            },
38
        }),
39
        [updatePositionCallbackFn]
40
    )
41

    
42
    return (
43
        <Marker
44
            draggable={true}
45
            position={currentPosition}
46
            icon={getMapPointIcon(mapPoint)}
47
            eventHandlers={eventHandlers}
48
            ref={markerRef}
49
        >
50
            {children}
51
        </Marker>
52
    )
53
}
54

    
55
export default MapMarker
(1-1/3)