forked from Leaflet/Leaflet.draw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
168 lines (144 loc) · 5.49 KB
/
index.html
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!-- LEAFLET SOURCE -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.9.4/leaflet.min.js"
integrity="sha512-puJW3E/qXDqYp9IfhAI54BJEaWIfloJ7JWs7OeD5i6ruC9JZL1gERT1wjtwXFlh7CjE7ZJ+/vcRZRkIYIb6p4g=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.9.4/leaflet.min.css"
integrity="sha512-h9FcoyWjHcOcmEVkxOfTLnmZFWIH0iZhZT1H2TbOq55xssQGEJHEaIm+PgoUaZbRvQTNTluNOEfb1ZRy6D3BOw=="
crossorigin="anonymous" referrerpolicy="no-referrer"/>
<!-- Your Leaflet Draw build -->
<script type="text/javascript" src="./dist/leaflet.draw-src.js"></script>
<link rel="stylesheet" href="./dist/leaflet.draw-src.css">
</head>
<body style="display: flex; justify-content: center; flex-direction: column; align-items: center;gap: 5px">
<div id="map" style="width: 95vw; height: 80vh"></div>
<div style="display: flex; gap: 35px; border: 1px solid black; padding: 5px">
<button type="button" id="btn-polyline">PolyLine</button>
<button type="button" id="btn-polygon">Polygon</button>
<button type="button" id="btn-rectangle">Rectangle</button>
<button type="button" id="btn-marker">Marker</button>
<button type="button" id="btn-circlemarker">CircleMarker</button>
</div>
<div style="border: 1px solid black;padding: 5px">
<button type="button" id="btn-edit">Edit mode</button>
<button type="button" id="btn-remove">Delete mode</button>
</div>
<div style="border: 1px solid black;padding: 5px">
<button type="button" id="btn-save" disabled>Save</button>
<button type="button" id="btn-cancel" disabled>Cancel</button>
<button type="button" id="btn-finish" disabled>Finish</button>
<button type="button" id="btn-undo" disabled>Delete last vertice</button>
<button type="button" id="btn-removeAllLayers" disabled>Remove all layers</button>
</div>
</body>
<script type="text/javascript">
let osm = L.tileLayer("http://{s}.tile.osm.org/{z}/{x}/{y}.png", {
attribution: "© <a href=\"http://osm.org/copyright\">OpenStreetMap</a> contributors"
});
let map = new L.Map("map", {
layers: [osm],
center: new L.LatLng(-37.7772, 175.2756),
zoom: 15,
renderer: L.canvas()
});
let editableLayers = new L.FeatureGroup();
map.addLayer(editableLayers);
let options = {
position: "topright",
draw: {
externalButtons: {
polyline: document.getElementById("btn-polyline"),
polygon: document.getElementById("btn-polygon"),
rectangle: document.getElementById("btn-rectangle"),
marker: document.getElementById("btn-marker"),
circlemarker: document.getElementById("btn-circlemarker"),
actions: {
finish: document.getElementById("btn-finish"),
cancel: document.getElementById("btn-cancel"),
undo: document.getElementById("btn-undo")
}
},
polyline: {
shapeOptions: {
color: "#ff0000",
weight: 5
},
showLength: true,
icon: new L.DivIcon({
iconSize: new L.Point(8, 8),
className: 'leaflet-div-icon leaflet-editing-icon'
}),
},
polygon: {
allowIntersection: false, // Restricts shapes to simple polygons
showArea: true,
drawError: {
color: "#ff0000", // Color the shape will turn when intersects
message: "<strong>Oh snap!<strong> you can't draw that!" // Message that will show when intersect
},
shapeOptions: {
color: "#0000ff"
},
icon: new L.DivIcon({
iconSize: new L.Point(9, 9),
className: 'leaflet-div-icon leaflet-editing-icon'
}),
},
circle: false, // Turns off this drawing tool
rectangle: {
shapeOptions: {
clickable: true,
editable: false,
}
}
},
edit: {
externalButtons: { // To deactivate the edit Tools, just make this object empty (ex.: externalButtons: {}) and edit and remove equals false
edit: document.getElementById("btn-edit"),
// remove: document.getElementById("btn-remove"),
actions: {
save: document.getElementById("btn-save"),
cancel: document.getElementById("btn-cancel"),
// clearAll: document.getElementById("btn-removeAllLayers") // You can bind only the buttons you'll use
}
},
featureGroup: editableLayers, //REQUIRED!!
// edit: false,
remove: false
}
};
let drawControl = new L.Control.Draw(options);
map.addControl(drawControl);
map.on(L.Draw.Event.CREATED, function (e) {
let type = e.layerType,
layer = e.layer;
if (type === "marker") {
layer.bindPopup("A popup!");
}
editableLayers.addLayer(layer);
});
</script>
<script type="text/javascript">
const actions_btn = {
finish: document.getElementById("btn-finish"),
cancel: document.getElementById("btn-cancel"),
undo: document.getElementById("btn-undo"),
save: document.getElementById("btn-save"),
clearAll: document.getElementById("btn-removeAllLayers")
};
map.addEventListener("draw:actionstart", (e) => {
for (let action in e.actions) {
actions_btn[action].disabled = !e.actions[action];
}
});
map.addEventListener("draw:actionstop", (e) => {
for (let btn in actions_btn) {
actions_btn[btn].disabled = true;
}
});
</script>
</html>