-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenLayers.js
208 lines (160 loc) · 5.06 KB
/
openLayers.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
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
//Information about OpenLayers:
//API - http://openlayers.org/en/master/apidoc/index.html
//Examples - https://openlayers.org/en/latest/examples/
function init() {
//The bounding box of New York streets layer, add the bounding box coordinates in the array
var extent = [13, 55, 13, 55];
//Initial view
var view = new ol.View({
center: ol.proj.transform([13.19, 55.705], 'EPSG:4326', 'EPSG:3857'),
zoom: 15
});
//WMS sources
var public_buildings = new ol.source.ImageWMS({
url: ' http://stark.nateko.lu.se:8080/geoserver/wms',
params: {
'LAYERS': 'APAlayers:public_buildings_wgs84'
},
serverType: 'geoserver'
});
var roads_highway = new ol.source.ImageWMS({
url: ' http://stark.nateko.lu.se:8080/geoserver/wms',
params: {
'LAYERS': 'APAlayers:roads_highway_wgs84'
},
serverType: 'geoserver'
});
var adresses = new ol.source.ImageWMS({
url: ' http://stark.nateko.lu.se:8080/geoserver/wms',
params: {
'LAYERS': 'addresses_wgs84'
},
serverType: 'geoserver'
});
var roads_throug = new ol.source.ImageWMS({
url: ' http://stark.nateko.lu.se:8080/geoserver/wms',
params: {
'LAYERS': 'roads_throug_wgs84'
},
serverType: 'geoserver'
});
var rural_buildings = new ol.source.ImageWMS({
url: ' http://stark.nateko.lu.se:8080/geoserver/wms',
params: {
'LAYERS': 'rural_buildings_wgs84'
},
serverType: 'geoserver'
});
var districts = new ol.source.ImageWMS({
url: ' http://stark.nateko.lu.se:8080/geoserver/wms',
params: {
'LAYERS': 'districts_wgs84'
},
serverType: 'geoserver'
});
var railroads = new ol.source.ImageWMS({
url: ' http://stark.nateko.lu.se:8080/geoserver/wms',
params: {
'LAYERS': 'railroads_wgs84'
},
serverType: 'geoserver'
});
function updateLayers(evt) {
var e = target.getElementById();
alert e;
}
//OpenStreetMap background layer and New York streets layer
var layers = [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
new ol.layer.Image({
source: public_buildings
}),
new ol.layer.Image({
source: roads_highway
}),
new ol.layer.Image({
source: adresses
}),
new ol.layer.Image({
source: roads_throug
}),
new ol.layer.Image({
source: rural_buildings
}),
new ol.layer.Image({
source: railroads
}),
];
//Bind the map object to our "map" div and add some extra functionality
var map = new ol.Map({
layers: layers,
controls: ol.control.defaults({
attributionOptions:
({
collapsible: false
})
}).extend([
//Extra functionality of the map
//Control for displaying coordinates
new ol.control.MousePosition({
coordinateFormat: ol.coordinate.createStringXY(4),
projection: 'EPSG:4326'
}),
//Control for displaying a scale line
new ol.control.ScaleLine({
target: document.getElementById('scale-line')
}),
//Control for zoming to a defined extent
new ol.control.ZoomToExtent({
extent: ol.proj.transform(extent, 'EPSG:4326', 'EPSG:3857')
})
]),
target: 'map',
view: view
});
//Add click event for getting attributes from WMS
map.on('singleclick', function (evt) {
var viewResolution = /** @type {number} */ (view.getResolution());
var url = wmsSource.getGetFeatureInfoUrl(
evt.coordinate, viewResolution, 'EPSG:3857',
{ 'INFO_FORMAT': 'application/json' });
// JQuery HTTP GET request
$.get(url, function (resp) {
var features = resp.features;
if (features.length > 0) {
var properties = features[0].properties;
fillInfoPanel(properties)
}
})
});
// Add function that fills the panel with attributes collected by the click event
function fillInfoPanel(props) {
var infoPanel = document.getElementById('infoContent');
var content = ''
var heading = '';
var listBegin = '<ul>'
var listItems = '';
var listEnd = '</ul>'
for (var prop in props) {
// skip loop if the property is from prototype
if (!props.hasOwnProperty(prop)) continue;
listItems += '<li>' + '<b>' + prop + "</b> : " + props[prop] + '</li>';
}
content = heading + listBegin + listItems + listEnd;
infoPanel.innerHTML = content;
}
// Toggles the info panel overlaying the map
toggleInfo = function() {
var checkBox = document.getElementById('infoToggle');
var checked = checkBox.checked;
var display = ''
if (checked) {
display = 'block'
} else {
display = 'none'
}
document.getElementById('info').style.display = display
}
}