-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
411 lines (341 loc) · 15.1 KB
/
script.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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
$(document).ready(function() {
/* Fetch saved places from local storage */
if (typeof(Storage) !== 'undefined') {
// count = number of places stored
// places = the stored places
if (localStorage.count) {
if (localStorage.places) {
var jsonObj = JSON.parse(localStorage.places);
for (var i = 0; i < Number(localStorage.count); i++) {
var cur = JSON.parse(jsonObj[i]);
var item = '<li><a href="#map" data-lat="'+cur.lat+'" data-long="'+cur.long+'" data-type="'+cur.type+'"><h2>'+cur.title+'</h2><p class="ui-li-aside"><button id="removePlace" data-inline="true" data-icon="delete" data-iconpos="notext" data-id="'+i+'"></button></p></a></li>';
$('#myPlaces').append(item);
}
$('#myPlaces').listview('refresh');
$('#home').trigger('create'); // refresh jQM controls
if (i > 0) {
$('#noPlaces').hide();
}
}
} else {
localStorage.count = 0;
}
}
/* Initialize map */
var currentPosition = new google.maps.LatLng(60.18711,24.83192);
var directionsDisplay = new google.maps.DirectionsRenderer();
var directionsService = new google.maps.DirectionsService();
var mapOptions = {
zoom: 15,
center: currentPosition,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
directionsDisplay.setMap(map);
var markersArray = []; // store all map markers here
var curLoc = null; // Current user-entered location
var curLocInfoOn = null;
var curLocInfoOff = null;
var curLocInfoWindow = null;
/******
* Listener for setting your current location (marker) on the map by clicking.
* Keeps track of one current location, adding and removing listeners each time
* the location is changed with another click.
*/
google.maps.event.addListener(map, 'click', function(event) {
var marker = new google.maps.Marker({
map: map,
position: event.latLng,
title: 'You are here.',
});
if (curLoc != null) {
removeCurLoc();
}
curLoc = marker; // Overwrites any previously set location
var infoText = 'You are here. Click to remove.';
curLocRemoval = google.maps.event.addListener(marker, 'click', function() {
removeCurLoc();
});
curLocInfoOn = google.maps.event.addListener(marker, 'mouseover', function() {
curLocInfoWindow = new google.maps.InfoWindow({content: infoText});
curLocInfoWindow.open(map, this);
});
curLocInfoOff = google.maps.event.addListener(marker, 'mouseout', function() {
curLocInfoWindow.close();
});
});
// Function for clearing the current location listeners and the current location marker
function removeCurLoc() {
google.maps.event.removeListener(curLocInfoOn);
google.maps.event.removeListener(curLocInfoOff);
google.maps.event.removeListener(curLocRemoval);
curLoc.setMap(null);
}
// function for removing markers
function removeMarkers() {
if (markersArray) {
for (var i = 0; i < markersArray.length; i++ ) {
markersArray[i].setMap(null);
}
markersArray.length = 0;
}
}
// resize the map when the map page is shown
$('#map').on('pageshow', function() {
google.maps.event.trigger(map, 'resize');
map.setCenter(currentPosition);
});
/* Execute a sparql query and show the results in a list */
$(document).on('click', '#searchButton', function() {
var keyword = $('#searchField').val();
if (keyword == '') {
return;
}
/* Search people */
var sparqlQuery = 'prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> \
prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> \
prefix foaf: <http://xmlns.com/foaf/0.1/> \
prefix db:<http://dbpedia.org/ontology/> \
prefix tisc:<http://observedchange.com/tisc/ns#> \
SELECT DISTINCT ?name ?label ?address ?lat ?long WHERE{ \
?person a foaf:Person; \
foaf:name ?name; \
foaf:firstName ?firstName; \
foaf:familyName ?familyName . \
{?person foaf:name ?name FILTER regex(?name, "'+keyword+'", "i")} \
UNION {?person foaf:firstName ?firstName FILTER regex(?firstName, "'+keyword+'", "i")} \
UNION {?person foaf:familyName ?familyName FILTER regex(?familyName, "'+keyword+'", "i")}. \
?peopleorganization foaf:member ?person . \
?peopleorganization rdfs:label ?label . \
?buildingorg rdfs:label ?label2 . \
FILTER(str(?label)=str(?label2)) \
?buildingorg tisc:locatedAt ?building . \
?building db:address ?address . \
?building geo:lat ?lat . \
?building geo:long ?long . \
} \
GROUP BY ?name ?label ?address ?lat ?long';
var encodedQuery = encodeURIComponent(sparqlQuery);
/* Search courses */
var sparqlQuery2 = 'prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> \
prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> \
prefix foaf: <http://xmlns.com/foaf/0.1/> \
prefix db:<http://dbpedia.org/ontology/> \
prefix tisc:<http://observedchange.com/tisc/ns#> \
prefix aiiso:<http://purl.org/vocab/aiiso/schema#> \
prefix teach:<http://linkedscience.org/teach/ns#> \
SELECT DISTINCT ?code ?courseTitle ?label ?address ?lat ?long WHERE{ \
?course a aiiso:Course; \
aiiso:code ?code; \
teach:courseTitle ?courseTitle . \
{?course aiiso:code ?code FILTER regex(?code, "'+keyword+'", "i")} \
UNION {?course teach:courseTitle ?courseTitle FILTER regex(?courseTitle, "'+keyword+'", "i")} . \
?courseorganization aiiso:teaches ?course . \
?courseorganization foaf:name ?label . \
?buildingorg rdfs:label ?label2 . \
FILTER(str(?label)=str(?label2)) \
?buildingorg tisc:locatedAt ?building . \
?building db:address ?address . \
?building geo:lat ?lat . \
?building geo:long ?long \
} \
GROUP BY ?code ?courseTitle ?label ?address ?lat ?long';
var encodedQuery2 = encodeURIComponent(sparqlQuery2);
/* Search departments and buildings */
var sparqlQuery3 = 'prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> \
prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> \
prefix db:<http://dbpedia.org/ontology/> \
prefix tisc:<http://observedchange.com/tisc/ns#> \
prefix aiiso:<http://purl.org/vocab/aiiso/schema#> \
SELECT DISTINCT ?label ?buildingname ?address ?lat ?long \
WHERE{ \
?department a aiiso:Department; \
rdfs:label ?label; \
tisc:locatedAt ?building . \
?building db:address ?address; \
rdfs:label ?buildingname; \
geo:lat ?lat; \
geo:long ?long . \
{?department rdfs:label ?label FILTER regex(?label, "'+keyword+'", "i")} \
UNION {?building rdfs:label ?buildingname FILTER regex(?buildingname, "'+keyword+'", "i")} \
} \
GROUP BY ?label ?buildingname ?address ?lat ?long \
ORDER BY ?label';
var encodedQuery3 = encodeURIComponent(sparqlQuery3);
$.mobile.loading('show'); // show loading icon
$.when( $.ajax({ url: 'http://data.aalto.fi/sparql?query='+encodedQuery+'&output=json',
cache: false, dataType: 'jsonp' }),
$.ajax({ url: 'http://data.aalto.fi/sparql?query='+encodedQuery2+'&output=json',
cache: false, dataType: 'jsonp' }),
$.ajax({ url: 'http://data.aalto.fi/sparql?query='+encodedQuery3+'&output=json',
cache: false, dataType: 'jsonp' })
).then(function(results1, results2, results3) {
$('#myPlaces').hide();
$('#noPlaces').hide();
$('#searchResults').show();
$('#searchResults, #noResults').html('');
// people
var data1 = results1[0];
var bindings1 = data1.results.bindings;
if (!($.isEmptyObject(bindings1))) {
var item = '<li data-role="list-divider">People</li>';
$('#searchResults').append(item);
}
for (i in bindings1) {
var item = '<li><a href="#map" data-lat="'+bindings1[i].lat.value+'" data-long="'+bindings1[i].long.value+'" data-type="person"><h2>'+bindings1[i].name.value+'</h2><p>'+bindings1[i].label.value+'</p><p class="ui-li-aside"><button id="addPlace" data-inline="true" data-icon="plus" data-iconpos="notext"></button></p></a></li>';
$('#searchResults').append(item);
}
// courses
var data2 = results2[0];
var bindings2 = data2.results.bindings;
if (!($.isEmptyObject(bindings2))) {
var item = '<li data-role="list-divider">Courses</li>';
$('#searchResults').append(item);
}
for (i in bindings2) {
var item = '<li><a href="#map" data-lat="'+bindings2[i].lat.value+'" data-long="'+bindings2[i].long.value+'" data-type="course"><h2>'+bindings2[i].code.value+' '+bindings2[i].courseTitle.value+'</h2><p>'+bindings2[i].label.value+'</p><p class="ui-li-aside"><button id="addPlace" data-inline="true" data-icon="plus" data-iconpos="notext"></button></p></a></li>';
$('#searchResults').append(item);
}
// departments and buildings
var data3 = results3[0];
var bindings3 = data3.results.bindings;
if (!($.isEmptyObject(bindings3))) {
var item = '<li data-role="list-divider">Departments and buildings</li>';
$('#searchResults').append(item);
}
for (i in bindings3) {
var item = '<li><a href="#map" data-lat="'+bindings3[i].lat.value+'" data-long="'+bindings3[i].long.value+'" data-type="dep"><h2>'+bindings3[i].buildingname.value+', '+bindings3[i].label.value+'</h2><p>'+bindings3[i].address.value+'</p><p class="ui-li-aside"><button id="addPlace" data-inline="true" data-icon="plus" data-iconpos="notext"></button></p></a></li>';
$('#searchResults').append(item);
}
$('#searchResults').listview('refresh');
$('#home').trigger('create'); // refresh jQM controls
$.mobile.loading('hide');
// if there are no results
if ($('#searchResults').is(':empty')) {
$('#noResults').html('<p><em>No results</em></p>');
}
});
});
/* Hide search results and show my places when clear button is clicked */
$(document).on('click', '.ui-input-clear', function() {
$('#searchResults').html('');
$('#myPlaces').show();
if (Number(localStorage.count) == 0) {
$('#noPlaces').show();
}
});
/* Add place to my places */
$(document).on('click', '#addPlace', function() {
if (typeof(Storage) !== 'undefined') {
var newObj = { 'lat': $(this).closest('a').attr('data-lat'),
'long': $(this).closest('a').attr('data-long'),
'type': $(this).closest('a').attr('data-type'),
'title': $(this).closest('a').children('h2')[0].innerHTML };
var jsonObj;
if (localStorage.places) {
jsonObj = JSON.parse(localStorage.places);
} else {
jsonObj = JSON.parse('{}');
}
jsonObj[localStorage.count] = JSON.stringify(newObj);
jsonString = JSON.stringify(jsonObj);
localStorage.places = jsonString;
var cur = JSON.parse(jsonObj[Number(localStorage.count)]);
var item = '<li><a href="#map" data-lat="'+cur.lat+'" data-long="'+cur.long+'" data-type="dep"><h2>'+cur.title+'</h2><p class="ui-li-aside"><button id="removePlace" data-inline="true" data-icon="delete" data-iconpos="notext"></button></p></a></li>';
$('#myPlaces').append(item);
$('#myPlaces').listview('refresh');
$('#home').trigger('create'); // refresh jQM controls
$('#noPlaces').hide();
localStorage.count = Number(localStorage.count) + 1;
} else {
alert("Local storage is not supported.");
}
return false;
});
/* Remove place from my places */
$(document).on('click', '#removePlace', function() {
if (typeof(Storage) !== 'undefined') {
if (localStorage.places && Number(localStorage.count) > 0) {
jsonObj = JSON.parse(localStorage.places);
delete jsonObj[$(this).attr('data-id')];
jsonString = JSON.stringify(jsonObj);
localStorage.places = jsonString;
$(this).closest('li').remove();
$('#myPlaces').listview('refresh');
$('#home').trigger('create'); // refresh jQM controls
localStorage.count = Number(localStorage.count) - 1;
if (Number(localStorage.count) == 0) {
$('#noPlaces').show();
}
}
} else {
alert("Local storage is not supported.");
}
return false;
});
/* Add place marker on map when a link is clicked */
$(document).on('click', '#searchResults a, #myPlaces a, #restaurants a', function() {
var lat = $(this).attr('data-lat');
var long = $(this).attr('data-long');
var type = $(this).attr('data-type');
if (!type) {
type = 'marker';
}
removeMarkers();
var marker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(lat, long),
title: $(this).text(),
icon: 'images/' + type + '.png',
shadow: 'images/' + type + '_shadow.png'
});
markersArray.push(marker);
currentPosition = marker.getPosition();
var title = '<p>'+$(this).closest('a').children('h2')[0].innerHTML+'</p>';
var directionsAnchor = '<a id="directionsLink" href="#">Show the path here</a>';
google.maps.event.addListener(marker, 'click', function() {
var infoWindow = new google.maps.InfoWindow({content: title+ directionsAnchor});
infoWindow.open(map, this);
});
});
/* Get directions from current location to target */
$(document).on('click', '#directionsLink', function() {
if (curLoc != null && curLoc.getMap() != null) {
var start = curLoc.getPosition();
var end = markersArray[0].getPosition();
var request = {
origin: start,
destination: end,
travelMode: google.maps.TravelMode.WALKING
};
directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
}
});
} else if ('geolocation' in navigator) {
$.mobile.loading('show');
navigator.geolocation.getCurrentPosition(function(position) {
var start = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var end = markersArray[0].getPosition();
var request = {
origin: start,
destination: end,
travelMode: google.maps.TravelMode.WALKING
};
directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
}
});
$.mobile.loading('hide');
});
} else {
alert("I'm sorry, but geolocation services are not supported by your browser.");
}
});
/* TODO: Fetch Aalto events */
$(document).on('click', '#fetchAaltoEvents', function() {
alert('Not implemented.');
});
});