-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
62 lines (56 loc) · 1.61 KB
/
index.js
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
import MapboxDraw from "@mapbox/mapbox-gl-draw";
import DrawRectangle, {
DrawStyles,
} from "mapbox-gl-draw-rectangle-restrict-area";
const OSM_STYLE = {
version: 8,
sources: {
osm: {
type: "raster",
tiles: [
"https://a.tile.openstreetmap.org/{z}/{x}/{y}.png",
"https://b.tile.openstreetmap.org/{z}/{x}/{y}.png",
"https://c.tile.openstreetmap.org/{z}/{x}/{y}.png",
],
tileSize: 256,
},
},
layers: [
{
id: "osm",
source: "osm",
type: "raster",
},
],
};
const map = new mapboxgl.Map({
container: "map", // container id
style: OSM_STYLE,
center: [-91.874, 42.76], // starting position
zoom: 12, // starting zoom
});
const draw = new MapboxDraw({
userProperties: true,
displayControlsDefault: false,
styles: DrawStyles,
modes: Object.assign(MapboxDraw.modes, {
draw_rectangle: DrawRectangle,
}),
});
map.addControl(draw);
const currenArea = document.getElementById("area");
currenArea.textContent = "0";
function onAreaChanged(area) {
currenArea.textContent = `${(area / 1_000_000).toFixed(2)}`;
}
document.getElementById("draw-rectangle").addEventListener("click", () => {
console.log("let's draw!");
draw.changeMode("draw_rectangle", {
areaLimit: 5 * 1_000_000, // 5 km2, optional
escapeKeyStopsDrawing: true, // default true
allowCreateExceeded: false, // default false
exceedCallsOnEachMove: false, // default false - calls exceedCallback on each mouse move
exceedCallback: (area) => console.log(`area exceeded! ${area.toFixed(2)}`), // optional
areaChangedCallback: onAreaChanged,
});
});