1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
function MapComponent() {
const [map, setMap] = useState(null);
let draw, snap; // global so we can remove them later
const source = new VectorSource();
const vector = new VectorLayer({
source: source,
style: {
'fill-color': 'rgba(255, 255, 255, 0.2)',
'stroke-color': '#ffcc33',
'stroke-width': 2,
'circle-radius': 7,
'circle-fill-color': '#ffcc33',
},
});
useEffect(() => {
const osmLayer = new TileLayer({
preload: Infinity,
source: new OSM(),
})
const map = new Map({
target: "map",
layers: [osmLayer, vector],
view: new View({
center: [0, 0],
zoom: 0,
}),
});
const modify = new Modify({source: source});
map.addInteraction(modify);
setMap(map)
return () => map.setTarget(null)
}, []);
function addInteractions() {
draw = new Draw({
source: source,
type: "Polygon",
});
map.addInteraction(draw);
snap = new Snap({source: source});
map.addInteraction(snap);
}
return (
<Box>
<Toolbar>
<IconButton onClick={addInteractions}>
<MenuIcon />
</IconButton>
</Toolbar>
<div style={{height:'600px',width:'50%'}} id="map" className="map-container" />
</Box>
);
} |
Partager