-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
385 lines (320 loc) · 12.2 KB
/
index.html
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="https://api.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.js"></script>
<script src="https://unpkg.com/@mapbox/mapbox-sdk@0.11.0/umd/mapbox-sdk.min.js"></script>
<script src="https://unpkg.com/es6-promise@4.2.4/dist/es6-promise.auto.min.js"></script>
<script src="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.5.1/mapbox-gl-geocoder.min.js"></script>
<script src="https://npmcdn.com/@turf/turf@5.1.6/turf.min.js"></script>
<script src="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-directions/v4.1.0/mapbox-gl-directions.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.css" rel="stylesheet">
<link href="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.5.1/mapbox-gl-geocoder.css" rel="stylesheet">
<link href="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-directions/v4.1.0/mapbox-gl-directions.css" rel="stylesheet">
<link href="style.css" rel="stylesheet">
</head>
<body>
<div id="map"></div>
<div class="map-overlay " id="title">
<h1>Welcome to Melbourne</h1>
<h2>The world's most liveable city</h2>
</div>
<!-- <div id="info">
<p>Hover over a building!</p>
</div> -->
<div class="searchContainer">
<label>Search</label>
<input
id="searchboxInput"
placeholder="Please Text Your Keywords"
/>
<button type="button" id='searchButton' style = "float: right;">Go Now</button>
<label class="searchError" id="searchError"></label>
</div>
<div class="highlightContainer" id="selector">
<fieldset>
<label>Select an object</label>
<select id="layer" name="layer">
<option value="Free Tourist Tram">Free Tourist Tram</option>
<option value="water">Water</option>
<!-- <option value="buildings">Buildings</option> -->
<!-- <option value="Bicycle route">Bicycle route</option> -->
<option value="Open Space">Open Space</option>
<!-- <option value="landuse">Cafe & Restaurant </option> -->
</select>
</fieldset>
<fieldset>
<label>Choose a color</label>
<div id="swatches"></div>
</fieldset>
</div>
<div class="menuContainer" id="menu">
<input id="ckfxj13sl0ad419lhbebj5dsz" type="radio" name="rtoggle" value="streets" checked="checked"/>
<label for="customized">Normal</label>
<input id="ckgn3iijd0cfi19mphz3dy9ie" type="radio" name="rtoggle" value="light" />
<label for="light-v10">Light</label>
<input id="ckgne19bm0a3v19o0guxbz54f" type="radio" name="rtoggle" value="satellite" />
<label for="satellite-v9">Satellite</label>
</div>
<div id='showFilters'>
<div id="filter-group" class="filter-group"/>
</div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoiaW5mb3JtYXRpb252aXN1YWxpc2F0aW8yOCIsImEiOiJja2Z4aXVuOXkwMnV2MzNvZGF6czZ6YWZwIn0.FcC-iA3hoqKoU3O0YIK1HA';
// Set bounds to Melbourne
var bounds = [
[144.275297, -38.155227], // Southwest coordinates
[145.772345, -37.498547] // Northeast coordinates
];
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/informationvisualisatio28/ckfxj13sl0ad419lhbebj5dsz', // set for default map style
maxBounds: bounds // Sets bounds as max
});
//create a direction control
var dierctionsControl = new MapboxDirections({
accessToken: mapboxgl.accessToken
});
map.addControl(
dierctionsControl,
'top-right'
);
//set the initial bound to melbourne
map.fitBounds([[144.88, -37.81], [145.03, -37.81]]);
//create filter arrays
const filterElements = ['Must Visit', 'Recreation and Art', 'Parks', 'Stores', 'Buildings', 'Services', 'Transport', 'Schools', 'Fitness','Cafes and Restaurants'];
var imgElements = new Array(filterElements.length);
//search marker
var marker = new mapboxgl.Marker();
function reloadMap() {
//map.setStyle('mapbox://styles/informationvisualisatio28/ckfxj13sl0ad419lhbebj5dsz');
map.on('load', function() {
//use for loop to add 9 filters
for (let i = 0; i < filterElements.length; i++) {
let filterElement = filterElements[i];
// Add checkbox and label elements for the layer.
var input = document.createElement('input');
input.type = 'checkbox';
input.id = filterElement;
input.checked = false;
filterGroup.appendChild(input);
//set checkbox's label
var label = document.createElement('label');
label.setAttribute('for', filterElement);
label.textContent = filterElement;
filterGroup.appendChild(label);
//add img icon to the chekbox
imgElements[i] = document.createElement('img');
imgElements[i].src = 'img/Icon/' + filterElement + '.png';
imgElements[i].className = 'icon_img';
label.appendChild(imgElements[i]);
imgElements[i].style.visibility = 'hidden';
// add check event When the checkbox changes, update the visibility of the layer.
input.addEventListener('change', function(e) {
map.setLayoutProperty(
filterElement,
'visibility',
e.target.checked ? 'visible' : 'none'
);
if (e.target.checked) {
imgElements[i].style.visibility = 'visible';
}
else {
imgElements[i].style.visibility = 'hidden';
}
});
// Create a popup, but don't add it to the map yet.
var popup;
map.on('mouseenter', filterElement, function(e) {
map.getCanvas().style.cursor = 'pointer';
// add popup elements
var popDiv = document.createElement('div');
var popImg = document.createElement('img');
// popImg.onerror = function() {
// this.src = 'img/poi-popup-icon/welcome-to-melbourne-green-road-sign.jpg'; // place your error.png image instead
// };
//add local image to pop
var imgPath = 'img/poi-popup-icon/' + e.features[0].properties.Feature_Na + '.jpg';
popImg.src = imgPath;
// let poiInfo = map.queryRenderedFeatures(e.point, {
// layers: filterElements
// })
popImg.className = 'pop_img';
if (e.features[0].sourceLayer == 'Cafes_and_restaurants-7a9tq4') {
popDiv.innerHTML = '<h3>Restaurant Name:</h3><p>' + e.features[0].properties.trading_name + '</p ><h4>' + e.features[0].properties.seating_type + '</h4>';
} else {
popDiv.innerHTML = '<h3>Name:</h3><p>' + e.features[0].properties.Feature_Na + '</p ><h4>' + e.features[0].properties.Filter + '</h4>';
}
popDiv.appendChild(popImg);
//if no picture, don't show picture
popImg.onerror = function() {
popDiv.removeChild(popImg); // place your error.png image instead
};
popup = new mapboxgl.Popup()
.setLngLat(e.lngLat)
.setDOMContent(popDiv)
.addTo(map);
});
// Change the mouse to a pointer icon when you mouse over a building
map.on('mouseleave', filterElement, function() {
map.getCanvas().style.cursor = '';
popup.remove();
});
//add popup, in pop, there are pictures and HTML string information
map.on('click', filterElement, function(e) {
var popDiv = document.createElement('div');
var popImg = document.createElement('img');
//popImg.src = 'https://upload.wikimedia.org/wikipedia/commons/7/7c/201408_cat.png';
popImg.onerror = function () {
this.src = 'img/poi-popup-icon/welcome-to-melbourne-green-road-sign.jpg'; // place your error.png image instead
};
//add local image to pop
var imgPath = 'img/poi-popup-icon/' + e.features[0].properties.Feature_Na + '.jpg';
popImg.src = imgPath;
//set pop's content
popImg.className = 'pop_img';
if (e.features[0].sourceLayer == 'Cafes_and_restaurants-7a9tq4'){
//popDiv.innerHTML = '<h3>Restaurant Name:</h3><p>' + e.features[0].properties.trading_name+ '</p><h4>'+ e.features[0].properties.seating_type + '</h4>';
popDiv.innerHTML = '<h3>Restaurant Name:</h3><p>';
} else {
popDiv.innerHTML = '<h3>Name:</h3><p>' + e.features[0].properties.Feature_Na + '</p><h4>' + e.features[0].properties.Filter + '</h4>';
}
popDiv.innerHTML = '<h3>Name:</h3><p>' + e.features[0].properties.Feature_Na + '</p><h4>' + e.features[0].properties.Filter + '</h4>' ;
popDiv.appendChild(popImg);
new mapboxgl.Popup()
.setLngLat(e.lngLat)
.setDOMContent(popDiv)
.addTo(map);
});
//remove search marker
map.on('click', function (e) {
marker.remove();
});
}
});
}
// add show and hide filter controller
var filterDiv = document.getElementById('showFilters');
//add the checbox element
var filterGroup = document.getElementById('filter-group');
// added buttons
var selector = document.getElementById('selector');
var swatches = document.getElementById('swatches');
var layer = document.getElementById('layer');
var mapType = 'customized';
/*----- select highlighted map elements by color -----*/
var colors = [
'#ffffcc',
'#a1dab4',
'#41b6c4',
'#2c7fb8',
'#253494',
'#fed976',
'#feb24c',
'#fd8d3c',
'#f03b20',
'#bd0026'
];
colors.forEach(function (color) {
var swatch = document.createElement('button');
swatch.style.backgroundColor = color;
swatch.addEventListener('click', function () {
if (layer.value == 'Free Tourist Tram')
map.setPaintProperty(layer.value, 'line-color', color);
else
map.setPaintProperty(layer.value, 'fill-color', color);
});
swatches.appendChild(swatch);
});
/*-------- radio selection for changing map styles -------*/
var layerList = document.getElementById('menu');
var inputs = layerList.getElementsByTagName('input');
function switchLayer(layer) {
var layerId = layer.target.id;
mapType = layerId;
//filterGroup.style.display="none";
map.setStyle('mapbox://styles/informationvisualisatio28/' + layerId);
//execute this part after 1 second, waiting for setStyle accomplishing
setTimeout(function (){
for (let i = 0; i < filterElements.length; i++) {
map.setLayoutProperty(
filterElements[i],
'visibility',
document.getElementById(filterElements[i]).checked ? 'visible': 'none'
);
}
directionsControl.removeRoutes();
map.removeControl(this.directionsControl);
map.addControl(
dierctionsControl,
'top-right'
);
//remove and add dierctionsControl (have to do it after change style)
//map.removeControl(dierctionsControl);
}, 1000);
if (layerId === 'ckgne19bm0a3v19o0guxbz54f') {
selector.style.display="none";
} else {
selector.style.display="block";
}
//reload the function of the map
//reloadMap();
}
for (var i = 0; i < inputs.length; i++) {
inputs[i].onclick = switchLayer;
}
// filter function
if (mapType === 'customized')
reloadMap();
/*------------- search function ---------------*/
var searchboxInput = document.getElementById('searchboxInput');
var searchButton = document.getElementById('searchButton');
var mapboxClient = mapboxSdk({ accessToken: mapboxgl.accessToken });
async function onSearch(text){
return await mapboxClient.geocoding
.forwardGeocode({
query: text,
autocomplete: false,
limit: 10,
// further limit results to the geographic bounds representing the region of Melbourne
bbox: [144.275297, -38.155227, 145.772345, -37.498547]
})
.send()
.then(function (response) {
if (
response &&
response.body &&
response.body.features &&
response.body.features.length
) {
var feature = response.body.features[0];
//clear the searchError info
document.getElementById("searchError").innerHTML = "";
map.flyTo({center: feature.center});
map.setZoom = 18;
/* map.setZoom(22) */;
//new mapboxgl.Marker().setLngLat(feature.center).addTo(map);
marker.setLngLat(feature.center).addTo(map);
return feature;
}
else {
document.getElementById("searchError").innerHTML = "Please search the place only in Melbourne";
}
});
}
searchButton.onclick = function () {
marker.remove();
var res = onSearch(searchboxInput.value);
}
//add users' location
map.addControl(
new mapboxgl.GeolocateControl({
positionOptions: {
enableHighAccuracy: true
},
trackUserLocation: true
})
);
</script>
</body>
</html>