/
routing.js
90 lines (71 loc) · 2.14 KB
/
routing.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
var options = {
epsg: "EPSG:3857",
bounds: [305346, 5163232, 321598, 5152970],
units: 'm'
};
var styles = {
'LineString': new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'green',
width: 10
})
}),
'MultiLineString': new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'green',
width: 5
})
})
};
var styleFunction = function(feature) {
return styles[feature.getGeometry().getType()];
};
// map initializing
gsc.map.create('map', options);
// reggio_emilia layer
// layers: http://hub.geosmartcity.eu/geoserver/web/
var osmSource = new ol.source.OSM();
var osmLayer = new ol.layer.Tile({
source: osmSource
});
gsc.map.addLayer(osmLayer);
var vectorLayer;
var arr = [];
gsc.map.olMap.on('click', function(e) {
var point = ol.proj.transform(e.coordinate, 'EPSG:3857', 'EPSG:4326');
if (arr.length > 1) {
arr.length = 0;
}
if (arr.length == 1) {
arr.push(point);
document.getElementById("x1").value = arr[0][0];
document.getElementById("y1").value = arr[0][1];
document.getElementById("x2").value = arr[1][0];
document.getElementById("y2").value = arr[1][1];
gsc.routing.routing(arr[0][0], arr[0][1], arr[1][0], arr[1][1])
.then(function(routeResponse) {
routeResponse.setStyle(styleFunction);
vectorLayer = routeResponse;
gsc.map.addLayer(routeResponse);
}, function(err) {
//console.log(err);
});
};
if (arr.length == 0) {
arr.push(point);
var x = point[0];
var y = point[1];
document.getElementById("x1").value = x;
document.getElementById("y1").value = y;
}
});
function myFunction() {
document.getElementById("x1").value = null;
document.getElementById("y1").value = null;
document.getElementById("x2").value = null;
document.getElementById("y2").value = null;
arr = [];
gsc.map.removeLayer(vectorLayer);
}
gsc.map.addMousePositionControl('coordinate');
gsc.map.addScaleBarControl('scalebar');