-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
219 lines (203 loc) · 5.66 KB
/
index.js
File metadata and controls
219 lines (203 loc) · 5.66 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import 'ol/ol.css';
import { Map, View, Feature } from 'ol';
import TileLayer from 'ol/layer/Tile';
import { Vector as VectorSource, XYZ } from 'ol/source';
import { Vector as VectorLayer } from 'ol/layer';
import { GeoJSON } from 'ol/format';
import { Fill, Circle, Stroke, Style, Icon } from 'ol/style';
import { transform, useGeographic } from 'ol/proj';
import { Point } from 'ol/geom';
let clicks = 0;
let user = { startPoint: [], endPoint: [] };
// Read flood barriers
let fb = require('./data/flood-barriers');
// Styles
var styles = {
'route': new Style({
stroke: new Stroke({
width: 6, color: [237, 212, 0, 0.8]
})
}),
'redPeg': new Style({
image: new Icon({
anchor: [0.5, 1],
src: 'assets/pin-red.png'
})
}),
'greenPeg': new Style({
image: new Icon({
anchor: [0.5, 1],
src: 'assets/pin-green.png'
})
})
};
// Markers
let greenPin = new VectorLayer({
source: new VectorSource(),
visible: true,
style: new Style({
image: new Circle({
radius: 7,
fill: new Fill({
color: '#1e753a'
})
})
})
});
let redPin = new VectorLayer({
source: new VectorSource(),
visible: true,
style: new Style({
image: new Circle({
radius: 7,
fill: new Fill({
color: '#fc0320'
})
})
})
});
// Lagos
const center = {
projection: 'EPSG:3857',
coords: transform([3.384082, 6.455027], 'EPSG:4326','EPSG:3857'),
zoom: 12
};
// Google maps
const google = new TileLayer({
type: 'base',
title: 'Google maps',
name: 'Google maps',
visible: true,
source: new XYZ({
url: 'https://mt1.google.com/vt/lyrs=m&hl=pt&x={x}&y={y}&z={z}'
})
});
const barriers = new VectorLayer({
source: new VectorSource({
features: (new GeoJSON({
defaultDataProjection: 'EPSG:4326',
featureProjection: 'EPSG:3857'
})).readFeatures(fb.getFloodBarriers())
}),
style: new Style({
stroke: new Stroke({
color: 'blue',
width: 3
}),
fill: new Fill({
color: 'rgba(0, 0, 255, 0.1)'
})
})
});
// Map object
const map = new Map({
target: 'map',
layers: [
google,
barriers
],
view: new View({
projection: center.projection,
center: center.coords,
zoom: center.zoom
})
});
//
map.on('singleclick', (e) => {
switch (clicks % 2) {
case 0:
console.log(e.coordinate);
user.startPoint = transform(e.coordinate, 'EPSG:3857', 'EPSG:4326');
map.removeLayer(greenPin);
greenPin.getSource().clear();
greenPin.getSource().addFeature(new Feature({
geometry: new Point(e.coordinate)
}));
map.addLayer(greenPin);
break;
case 1:
user.endPoint = transform(e.coordinate, 'EPSG:3857', 'EPSG:4326');
map.removeLayer(redPin);
redPin.getSource().clear();
redPin.getSource().addFeature(new Feature({
geometry: new Point(e.coordinate)
}));
map.addLayer(redPin);
break;
}
++clicks;
});
let showModalWindow = function(msg, type) {
let modal = document.getElementById("modal1");
let title = document.getElementById("modal-title");
let message = document.getElementById("modal-message");
switch (type) {
case 'warning':
title.innerHTML = `<strong>Warning!!</strong>`;
message.innerHTML = `<p>${msg}</p>`;
break;
case 'error':
title.innerHTML = `<strong>Error!!</strong>`;
message.innerHTML = `<p>${msg}</p>`;
}
modal.style.display = 'block';
};
let showWaitModal = function(show) {
let modal = document.getElementById("wait-modal");
if (show) {
modal.style.display = 'block';
} else {
modal.style.display = 'none';
}
};
// Source: https://hashnode.com/post/7-different-ways-to-make-ajax-calls-in-javascript-in-2019-cjr7pi2fl000gdjs2zgssqwhr
let route = null;
let getRoute = function(url) {
let data = [];
showWaitModal(true);
fetch(url, { method: 'get' })
.then(response => response.json())
.then(jsonData => {
showWaitModal(false);
// here we need to add the route to map
console.log(jsonData);
if (route) {
map.removeLayer(route);
}
route = new VectorLayer({
source: new VectorSource({
features: (new GeoJSON()).readFeatures(jsonData)
}),
style: new Style({
stroke: new Stroke({
color: 'red',
width: 3
})
})
});
map.addLayer(route);
})
.catch(err => {
showWaitModal(false);
showModalWindow('Got an error!!!!', 'error');
});
}
// Load route functionality
document.getElementById('route-button').onclick = function() {
if (user.startPoint.length != 0 && user.endPoint.length != 0) {
getRoute(`http://localhost:8081/cgi-bin/api.py?op=route&long0=${user.startPoint[0]}&lat0=${user.startPoint[1]}&long1=${user.endPoint[0]}&lat1=${user.endPoint[1]}`);
} else {
showModalWindow('You\'ve got no points', 'error');
}
};
// Close modal
document.getElementById('close-span').onclick = function() {
document.getElementById("modal1").style.display = "none";
};
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
let modal = document.getElementById("modal1");
if (event.target == modal) {
modal.style.display = "none";
}
}