-
Notifications
You must be signed in to change notification settings - Fork 0
/
AreaSelect.js
104 lines (96 loc) · 3.36 KB
/
AreaSelect.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
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
var map;
var isSelect = false;
var select = [];
var coords = [];
var selectStyle = L.divIcon({
iconSize: new L.Point(0, 0),
html: '<div class="circle selector"></div><div class="clickable"></div>'
});
L.Map.mergeOptions({
colors: [] // TODO
});
L.Map.addInitHook(function () {
map = this;
map.addEventListener("test");
});
L.Map.include({
setSelect: function(state) {
isSelect = state;
map.on('click', function(e) {
onMapClick(e);
});
},
getSelectedArea: function() {
return coords; // TODO return the bounds
}
});
function onMapClick(e) {
if(isSelect) {
l = select.length;
if(l < 2) {
select[l] = L.marker([e.latlng.lat, e.latlng.lng], {icon: selectStyle, draggable: true});
select[l].addTo(map);
select[l].id = l;
select[l].on('drag', dragBox);
if(l == 1) {
coord1 = select[0].getLatLng();
coord2 = select[1].getLatLng();
// Marker 1
l = select.length;
select[l] = L.marker([coord1.lat, coord2.lng], {icon: selectStyle, draggable: true});
select[l].id = l;
select[l].on('drag', dragBox);
select[l].addTo(map);
// Marker 2
l = select.length;
select[l] = L.marker([coord2.lat, coord1.lng], {icon: selectStyle, draggable: true});
select[l].id = l;
select[l].on('drag', dragBox);
select[l].addTo(map);
coords = [[coord1.lat, coord1.lng],
[coord2.lat, coord1.lng],
[coord2.lat, coord2.lng],
[coord1.lat, coord2.lng]];
var polygon = L.polygon(coords);
polygon.addTo(map);
l = select.length;
select[l] = polygon;
map.fire("selected");
}
}
}
}
function dragBox(e) {
id = e.target.id;
coord1 = select[id].getLatLng();
// Id's van de box markers
// 0 ------ 2
// | |
// 3 ------ 1
// vierkant zelf: 4
if(id == 0) { // update 2 en 3
select[2].setLatLng(new L.LatLng(coord1.lat, select[2].getLatLng().lng)); // u: lat
select[3].setLatLng(new L.LatLng(select[3].getLatLng().lat, coord1.lng)); // u: lng
}
if(id == 2) { // update 0 en 1
select[0].setLatLng(new L.LatLng(coord1.lat, select[0].getLatLng().lng)); // u: lat
select[1].setLatLng(new L.LatLng(select[1].getLatLng().lat, coord1.lng)); // u: lng
}
if(id == 3) { // update 0 en 1
select[1].setLatLng(new L.LatLng(coord1.lat, select[1].getLatLng().lng)); // u: lat
select[0].setLatLng(new L.LatLng(select[0].getLatLng().lat, coord1.lng)); // u: lng
}
if(id == 1) { // update 2 en 3
select[3].setLatLng(new L.LatLng(coord1.lat, select[3].getLatLng().lng)); // u: lat
select[2].setLatLng(new L.LatLng(select[2].getLatLng().lat, coord1.lng)); // u: lng
}
// update poly
coord1 = select[0].getLatLng();
coord2 = select[1].getLatLng();
coords = [[coord1.lat, coord1.lng],
[coord2.lat, coord1.lng],
[coord2.lat, coord2.lng],
[coord1.lat, coord2.lng]];
select[4].setLatLngs(coords);
map.fire("test");
}