|
| 1 | +import L from 'leaflet'; |
| 2 | +import { Circle, CircleMarker, Popup, Pane, useMap } from 'react-leaflet'; |
| 3 | +import { useSelector } from 'react-redux'; |
| 4 | +import { useLeafletContext, withPane } from '@react-leaflet/core'; |
| 5 | +import React, { useEffect, useRef, useState } from 'react'; |
| 6 | +import 'leaflet.vectorgrid'; |
| 7 | +import { BACKEND_VECTOR_TILES_URL } from '../../../utils/constants'; |
| 8 | + |
| 9 | +import { Button, Paper, Box, IconButton, Typography } from '@mui/material'; |
| 10 | +import { MapPopup } from '../MapSearch'; |
| 11 | + |
| 12 | +import { PopupStyle } from './styles'; |
| 13 | + |
| 14 | +export const DynamicStationsLayer = (props: any) => { |
| 15 | + const { selected_map } = useSelector((state: any) => state.map); |
| 16 | + const { |
| 17 | + selectCallback, |
| 18 | + selectedPoint, |
| 19 | + openCharts, |
| 20 | + zIndex, |
| 21 | + variable, |
| 22 | + data, |
| 23 | + url, |
| 24 | + } = props; |
| 25 | + const map = useMap(); |
| 26 | + const context = useLeafletContext(); |
| 27 | + // console.log(context.map.latLngToLayerPoint(selectedPoint.latlng)) |
| 28 | + // const [selected, setSelected] = useState<any>(null); |
| 29 | + |
| 30 | + useEffect(() => { |
| 31 | + let selected = false; |
| 32 | + // let hovered = false; |
| 33 | + const container = context.layerContainer || context.map; |
| 34 | + |
| 35 | + if (url) { |
| 36 | + context.map.createPane('stationssel'); |
| 37 | + // @ts-ignore |
| 38 | + context.map.getPane('stationssel').style.zIndex = zIndex; |
| 39 | + |
| 40 | + // @ts-ignore |
| 41 | + let vectorLayer = L.vectorGrid.protobuf(url, { |
| 42 | + interactive: true, |
| 43 | + pane: 'stationssel', |
| 44 | + vectorTileLayerStyles: { |
| 45 | + stationssel: (properties, zoom, geometryDimension) => { |
| 46 | + let opacity = 0.9; |
| 47 | + let color = data === 'future' ? '#abb2b9' : '#464b52'; |
| 48 | + // console.log(zoom, color, opacity) |
| 49 | + return { |
| 50 | + color: color, |
| 51 | + weight: data === 'future' ? 2 : 4, |
| 52 | + radius: data === 'future' ? 5 : 10, |
| 53 | + fill: true, |
| 54 | + fillOpacity: 0.7, |
| 55 | + opacity: opacity, |
| 56 | + }; |
| 57 | + }, |
| 58 | + }, |
| 59 | + }); |
| 60 | + |
| 61 | + context.map.addLayer(vectorLayer); |
| 62 | + vectorLayer.bringToFront(); |
| 63 | + |
| 64 | + return () => { |
| 65 | + // console.log('RETURN') |
| 66 | + try { |
| 67 | + // @ts-ignore |
| 68 | + if (vectorLayer) container.removeLayer(vectorLayer); |
| 69 | + } catch (e) { |
| 70 | + console.log('error REMOVING', e); |
| 71 | + } |
| 72 | + }; |
| 73 | + } |
| 74 | + }, [ |
| 75 | + selected_map, |
| 76 | + map, |
| 77 | + selectedPoint, |
| 78 | + context.layerContainer, |
| 79 | + context.map, |
| 80 | + selectCallback, |
| 81 | + url, |
| 82 | + ]); |
| 83 | + |
| 84 | + return ( |
| 85 | + selectedPoint && ( |
| 86 | + <Pane name="custom" style={{ zIndex: 1000 }}> |
| 87 | + <CircleMarker |
| 88 | + center={[selectedPoint.latlng.lat, selectedPoint.latlng.lng]} |
| 89 | + radius={2} |
| 90 | + pathOptions={{ color: '#164d36' }} |
| 91 | + > |
| 92 | + <Popup> |
| 93 | + <Box sx={PopupStyle}></Box> |
| 94 | + </Popup> |
| 95 | + </CircleMarker> |
| 96 | + </Pane> |
| 97 | + ) |
| 98 | + ); |
| 99 | +}; |
0 commit comments