|
| 1 | +import React from 'react'; |
| 2 | +import {Map, useControl} from 'react-map-gl/maplibre'; |
| 3 | +import {DeckProps} from 'deck.gl'; |
| 4 | +import {MapboxOverlay} from '@deck.gl/mapbox'; |
| 5 | +import { |
| 6 | + DrawLineStringMode, |
| 7 | + DrawPolygonMode, |
| 8 | + EditableGeoJsonLayer |
| 9 | +} from '@deck.gl-community/editable-layers'; |
| 10 | + |
| 11 | +function DeckGLOverlay(props: DeckProps) { |
| 12 | + const overlay = useControl<MapboxOverlay>(() => new MapboxOverlay(props)); |
| 13 | + overlay.setProps(props); |
| 14 | + return null; |
| 15 | +} |
| 16 | + |
| 17 | +export default function OverlaidEditable() { |
| 18 | + const [features, setFeatures] = React.useState({ |
| 19 | + type: 'FeatureCollection', |
| 20 | + features: [] |
| 21 | + }); |
| 22 | + const [mode, setMode] = React.useState(() => DrawPolygonMode); |
| 23 | + const [selectedFeatureIndexes] = React.useState([]); |
| 24 | + |
| 25 | + const layer = new EditableGeoJsonLayer({ |
| 26 | + data: features, |
| 27 | + mode, |
| 28 | + selectedFeatureIndexes, |
| 29 | + onEdit: ({updatedData}) => { |
| 30 | + //FIXME: this event is not fired... |
| 31 | + console.log('updatedData', updatedData); |
| 32 | + setFeatures(updatedData); |
| 33 | + } |
| 34 | + }); |
| 35 | + |
| 36 | + React.useEffect(() => { |
| 37 | + console.log(`mode changed to ${mode.name}`); |
| 38 | + }, [mode]); |
| 39 | + |
| 40 | + return ( |
| 41 | + <div> |
| 42 | + <Map |
| 43 | + style={{width: '100%', height: 500}} //FIXME: unable to get height 100% to work |
| 44 | + mapStyle={'https://demotiles.maplibre.org/style.json'} |
| 45 | + > |
| 46 | + <DeckGLOverlay layers={[layer]} /> |
| 47 | + </Map> |
| 48 | + <div className="controls"> |
| 49 | + <button |
| 50 | + className={`button ${mode === DrawLineStringMode ? 'active' : ''}`} |
| 51 | + onClick={() => setMode(() => DrawLineStringMode)} |
| 52 | + > |
| 53 | + Line |
| 54 | + </button> |
| 55 | + <button |
| 56 | + className={`button ${mode === DrawPolygonMode ? 'active' : ''}`} |
| 57 | + onClick={() => setMode(() => DrawPolygonMode)} |
| 58 | + > |
| 59 | + Polygon |
| 60 | + </button> |
| 61 | + </div> |
| 62 | + </div> |
| 63 | + ); |
| 64 | +} |
0 commit comments