-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
237 lines (186 loc) · 6.09 KB
/
app.js
File metadata and controls
237 lines (186 loc) · 6.09 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
/*
This application enables visualization of county development projects that have been mapped
on the ground and their status information updated periodically.
This application is based on openLayers 3.5
*/
/* create layer and their source instances */
//layers array to hold all layers
var layersArray = [];
//osm base layers
var osmlayer = new ol.layer.Tile({
source: new ol.source.MapQuest({layer:'osm'}),
//name: 'Openstreet map'
});
var satellite = new ol.layer.Tile({
source: new ol.source.MapQuest({layer:'sat'})
})
//subcounties boundaries layer
var boundarySource = new ol.source.TileWMS({
url: 'http://localhost:8080/geoserver/wms',
params: { 'LAYERS':'cpt:nairobi_sub_counties',transparent:'true'},
serverType:'geoserver',
tiled: true
});
var countybnd = new ol.layer.Tile({
source: boundarySource ,
name: 'County boundary'
});
/*project layers defined below*/
var allProjectsSource = new ol.source.TileWMS({
url: 'http://localhost:8080/geoserver/wms',
params:{'LAYERS':'cpt:allProjects'},
serverType:'geoserver'
});
var allProjects = new ol.layer.Tile({
source: allProjectsSource,
name: 'All projects'
});
allProjects.setVisible(false);
var ongoingProjectsSource = new ol.source.TileWMS({
url: 'http://localhost:8080/geoserver/wms',
params: { 'LAYERS':'cpt:ongoing_projects'},
serverType:'geoserver'
});
var ongoingProjects = new ol.layer.Tile({
source: ongoingProjectsSource,
name: 'Ongoing projects'
});
ongoingProjects.setVisible(false);
var completedProjectsSource = new ol.source.TileWMS({
url: 'http://localhost:8080/geoserver/wms',
params: { 'LAYERS':'cpt:completed_projects'},
serverType:'geoserver'
});
var completedProjects = new ol.layer.Tile({
source: completedProjectsSource,
name: 'Completed projeccts'
});
completedProjects.setVisible(false);
var proposedProjectsSource = new ol.source.TileWMS({
url: 'http://localhost:8080/geoserver/wms',
params: { 'LAYERS':'cpt:proposed_projects'},
serverType:'geoserver'
});
var proposedProjects = new ol.layer.Tile({
source: proposedProjectsSource,
name: 'Proposed projects'
});
proposedProjects.setVisible(false);
var _2013_2014_Source = new ol.source.TileWMS({
url: 'http://localhost:8080/geoserver/wms',
params: { 'LAYERS':'cpt:2013/2014'},
serverType:'geoserver'
});
var _2013_2014 = new ol.layer.Tile({
source:_2013_2014_Source,
name: '2013/2014'
});
_2013_2014.setVisible(false);
var _2014_2015_Source = new ol.source.TileWMS({
url: 'http://localhost:8080/geoserver/wms',
params: { 'LAYERS':'cpt:2014/2015'},
serverType:'geoserver'
});
var _2014_2015 = new ol.layer.Tile({
source: _2014_2015_Source,
name: '2014/2015'
});
_2014_2015.setVisible(false);
//adding layers to the layers array
layersArray.push(osmlayer); //0
layersArray.push(countybnd); //1
layersArray.push(ongoingProjects); //2
layersArray.push(proposedProjects); //3
layersArray.push(completedProjects); //4
layersArray.push(_2014_2015);//5
layersArray.push(_2013_2014);//6
//layersArray.push(allProjects); //5
//configure view properties for map instance
var view = new ol.View({
projection: 'EPSG:3857',//because osm base layer is in 'ESPG:3857'
center: ol.proj.transform([36.87,-1.28], 'EPSG:4326','EPSG:3857'),
zoom: 11
});
//access pop-up elements.
var container = document.getElementById('popup');
var content = document.getElementById('popup-content');
var closer = document.getElementById('popup-closer');
//click handler to hide/close pop-up
closer.onclick = function() {
overlay.setPosition(undefined);
closer.blur();
return false;
};
//Create overlay to anchor the popup to the map.
var overlay = new ol.Overlay( ({
element: container,
autoPan: true,
autoPanAnimation: {
duration: 250
}
}));
//control to display map coordinates
/*var mousePositionControl = new ol.control.MousePosition({
className: 'custom-mouse-position',
target: document.getElementById('location'),
projection:'EPSG:4326' ,
coordinateFormat: ol.coordinate.createStringXY(2),
undefinedHTML: ' '
});*/
var controls = [
new ol.control.Attribution(),
new ol.control.MousePosition({
projection: 'EPSG:4326',
coordinateFormat: function(coordinate) {
return ol.coordinate.format(coordinate, '{x}, {y}', 4);
}
}),
new ol.control.ScaleLine(),
new ol.control.Zoom(),
new ol.control.ZoomSlider()
//new ol.control.FullScreen()
];
//create the map instance
var map = new ol.Map({
renderer: 'canvas',//force canvas to be used
controls: controls,//ol.control.defaults().extend([mousePositionControl]),
layers: layersArray,
overlays: [overlay],
target:'map',
view: view
});
//function to manage layer visibility
function switchlayer(e){
/*for (var i = layersArray.length - 1; i >= 0; i--) {
layersArray[i]
}*/
layersArray[e.value].setVisible(e.checked);
}
/*this function changes the cursor to a pointer when cursor is on map*/
var cursorHoverStyle = "pointer";
var target = map.getTarget();
var mTarget = typeof target === "string" ? $("#"+target) : $(target);
map.on("pointermove", function (event) {
var mouseCoordInMapPixels = [event.originalEvent.offsetX, event.originalEvent.offsetY];
//get feature at mouse coords
var hit = map.forEachFeatureAtPixel(mouseCoordInMapPixels, function (feature, layer) {
return true;
});
if (hit) {
mTarget.css("cursor", cursorHoverStyle);
} else {
mTarget.css("cursor", "");
}
});
/*adds a click handler to the map to render the popup.*/
map.on('singleclick', function(evt) {
var coordinate = evt.coordinate;
var viewResolution = (view.getResolution());
var mapLayers = map.getLayers();
//variable to store requested feature info
var featureinfo = allProjectsSource.getGetFeatureInfoUrl(
coordinate, viewResolution,'EPSG:3857',
{'INFO_FORMAT': 'text/html'})
content.innerHTML = '<iframe seamless src="' + featureinfo + '"></iframe>' ;
overlay.setPosition(coordinate);
});