-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
355 lines (333 loc) · 9.46 KB
/
app.js
File metadata and controls
355 lines (333 loc) · 9.46 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
window.$ = require('jquery');
// import 'ol/ol.css';
import {
Map,
View
} from 'ol';
// layer
import TileLayer from 'ol/layer/Tile';
import OSM from 'ol/source/OSM';
import TileWMS from 'ol/source/TileWMS';
import XYZSource from 'ol/source/XYZ';
import GeoJSON from 'ol/format/GeoJSON';
import VectorLayer from 'ol/layer/Vector';
import VectorSource from 'ol/source/Vector';
import Overlay from 'ol/Overlay';
import {
fromLonLat,
toLonLat,
transformExtent
} from 'ol/proj';
import BingMaps from 'ol/source/BingMaps';
// control
import {
defaults as defaultControls,
Attribution,
FullScreen,
MousePosition,
OverviewMap,
ZoomToExtent,
ScaleLine,
ZoomSlider
} from 'ol/control';
import {
createStringXY
} from 'ol/coordinate';
const center = fromLonLat([100.5496, 16.9990], 'EPSG:3857');
var lyr = [];
const jsonPro = `http://119.59.125.189:8080/mapservice/gistnu/wms?service=WFS&version=1.0.0&request=GetFeature&typeName=gistnu:hospital&outputFormat=application%2Fjson`;
// base map
const layers = [
// tile layer
new TileLayer({
source: new OSM(),
title: 'osm',
zIndex: 0,
isBaseLayer: true,
visible: true
}),
// xyz tile
new TileLayer({
source: new XYZSource({
url: 'http://tile.stamen.com/terrain/{z}/{x}/{y}.jpg'
}),
title: 'terrain',
zIndex: 0,
isBaseLayer: true,
visible: false
}),
// google xyz tile
new TileLayer({
source: new XYZSource({
url: 'https://mt1.google.com/vt/lyrs=r&x={x}&y={y}&z={z}'
}),
title: 'groad',
zIndex: 0,
isBaseLayer: true,
visible: false
}),
new TileLayer({
source: new XYZSource({
url: 'https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}'
}),
title: 'ghybrid',
zIndex: 0,
isBaseLayer: true,
visible: false
}),
// bing map
new TileLayer({
preload: Infinity,
source: new BingMaps({
key: 'AvcFXgENVxj3HQY8YhCixoJrX_IwBzRil0jsOFCzD1EZjzZv88Wyumh7B5r_MhFg',
imagerySet: 'AerialWithLabels'
// use maxZoom 19 to see stretched tiles instead of the BingMaps
// "no photos at this zoom level" tiles
// maxZoom: 19
}),
title: 'bingmap',
zIndex: 0,
isBaseLayer: true,
visible: false
}),
// overlay layers
new TileLayer({
source: new TileWMS({
url: 'http://119.59.125.189:8080/mapservice/gistnu/wms?',
params: {
'LAYERS': 'gistnu:province',
'TILED': true
},
serverType: 'geoserver',
transition: 0
}),
title: 'province',
zIndex: 2,
isBaseLayer: false,
visible: true
}),
new TileLayer({
source: new TileWMS({
url: 'http://119.59.125.189:8080/mapservice/gistnu/wms?',
params: {
'LAYERS': 'gistnu:amphoe',
'TILED': true
},
serverType: 'geoserver',
transition: 0
}),
title: 'amphoe',
zIndex: 2,
isBaseLayer: false,
visible: false
}),
new TileLayer({
source: new TileWMS({
url: 'http://119.59.125.189:8080/mapservice/gistnu/wms?',
params: {
'LAYERS': 'gistnu:tambon',
'TILED': true
},
serverType: 'geoserver',
transition: 0
}),
title: 'tambon',
zIndex: 1,
isBaseLayer: false,
visible: false
}),
new TileLayer({
source: new TileWMS({
url: 'http://119.59.125.189:8080/mapservice/gistnu/wms?',
params: {
'LAYERS': 'gistnu:ways',
'TILED': true
},
serverType: 'geoserver',
transition: 0
}),
title: 'ways',
zIndex: 3,
isBaseLayer: false,
visible: false
}),
new TileLayer({
source: new TileWMS({
url: 'http://119.59.125.189:8080/mapservice/gistnu/wms?',
params: {
'LAYERS': 'gistnu:village',
'TILED': true
},
serverType: 'geoserver',
transition: 0
}),
title: 'village',
zIndex: 3,
isBaseLayer: false,
visible: false
}),
// ่json
new VectorLayer({
source: new VectorSource({
format: new GeoJSON(),
projection: 'EPSG:4326',
url: jsonPro
}),
title: 'hospital',
zIndex: 4,
isBaseLayer: false,
visible: false
}),
// vector tile
new VectorTileLayer({
declutter: true,
source: new VectorTileSource({
format: new MVT(),
url: 'http://119.59.125.189/mapservice/gwc/service/tms/1.0.0/' +
'gistnu:raintam2557@EPSG:900913@pbf/{z}/{x}/{-y}.pbf'
}),
style: new Style({
fill: new Fill({
color: 'red',
opacity: 0.2
}),
stroke: new Stroke({
color: 'white',
width: 1
})
})
})
]
// base layer switcher
$(':radio').change((e) => {
// console.log(e.target.value);
layers.forEach((l) => {
if (l.values_.isBaseLayer === true) {
if (l.values_.title === e.target.value) {
l.setVisible(true);
} else {
l.setVisible(false);
}
}
})
});
// overlay layers
$(':checkbox').click((e) => {
let chk = e.currentTarget.checked;
let lyr = e.currentTarget.defaultValue;
layers.forEach((l) => {
if (l.values_.title === e.currentTarget.defaultValue) {
if (e.currentTarget.checked === true) {
l.setVisible(true);
} else {
l.setVisible(false);
}
}
})
});
// attribution
const attribution = new Attribution({
collapsible: false,
// collapsed: true
});
// full screen
const fullScreen = new FullScreen();
// mouse position
const mousePosition = new MousePosition({
coordinateFormat: createStringXY(4),
projection: 'EPSG:4326',
className: 'custom-mouse-position',
target: 'mouse-position',
undefinedHTML: ' '
});
$('#projection').change((e) => {
mousePosition.setProjection(e.target.value);
});
$('#precision').change((e) => {
const format = createStringXY(e.target.valueAsNumber);
mousePosition.setCoordinateFormat(format);
});
// overview map
const overviewMap = new OverviewMap({
layers: [new TileLayer({
source: new OSM()
})]
});
// zoom to extent
const zoomExtent = new ZoomToExtent({
extent: transformExtent([100.2175, 16.7877, 100.3214, 16.8362], 'EPSG:4326', 'EPSG:3857')
});
// scale line
const scaleLine = new ScaleLine();
// zoom slidere
const zoomslider = new ZoomSlider();
// popup
// var container = document.getElementById('popup');
// var content = document.getElementById('popup-content');
// var closer = document.getElementById('popup-closer');
var container = $('#popup')[0];
var closer = $('#popup-closer')[0];
var overlay = new Overlay({
element: container,
autoPan: true,
autoPanAnimation: {
duration: 250
}
});
closer.onclick = function () {
overlay.setPosition(undefined);
closer.blur();
return false;
};
// create view
let view = new View({
center: center,
zoom: 9
})
// create map
const map = new Map({
target: 'map',
layers: layers,
view: view,
overlays: [overlay],
controls: defaultControls({
attribution: false
}).extend([
attribution,
fullScreen,
mousePosition,
overviewMap,
zoomExtent,
scaleLine,
zoomslider
])
});
map.on('singleclick', (e) => {
var coordinate = e.coordinate;
var lonlat = toLonLat(e.coordinate, 'EPSG:3857');
// console.log(toLonLat(e.coordinate, 'EPSG:3857'))
// content.innerHTML = '<p>You clicked here:</p><code>' + coordinate +
// '</code>';
let jsonUrl = `http://119.59.125.189:8080/mapservice/gistnu/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=gistnu%3Aamphoe&outputFormat=application%2Fjson&CQL_FILTER=INTERSECTS(geom,Point(${lonlat[0]} ${lonlat[1]}))`;
$.getJSON(jsonUrl, (res) => {
if (res.features.length >= 1) {
console.log(res.features);
let val = res.features[0].properties;
$('#popup-content').html(`<h2>อำเภอ ${val.amp_th}</h2><br>
จังหวัด ${val.prov_th}<br>
เนื้อที่ ${(val.area / 1000000).toFixed(2)} ตร.กม.
`);
overlay.setPosition(coordinate);
} else {
overlay.setPosition(undefined);
}
})
});
// attribution
function checkSize() {
var small = map.getSize()[0] < 600;
attribution.setCollapsible(small);
attribution.setCollapsed(small);
}
window.addEventListener('resize', checkSize);
checkSize();