-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
284 lines (234 loc) · 7.53 KB
/
script.js
File metadata and controls
284 lines (234 loc) · 7.53 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
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
window.onload = () => {
navigator.geolocation.getCurrentPosition(loadPlaces);}
//get current user location
/**
* @description This function takes the input coordinates from an oringin and a destination as arrays
* and calculates the distance from the origin point to that destination point
* @param {array} origin - the single point that is the origin to other points
* @param {array} dest - a single point to which the distance has to be calculated to
* @return {number} d - the distance
*/
var dist = function(origin, dest){
//get coordinates of point
var lon1 = origin[0];
var lat1 = origin[1];
var lon2 = dest[0];
var lat2 = dest[1];
//degrees to radiants
var R = 6371e3; // metres
var φ1 = lat1 * (Math.PI/180);
var φ2 = lat2 * (Math.PI/180);
var φ3 = lon1 * (Math.PI/180);
var φ4 = lon2 * (Math.PI/180);
var Δφ = (lat2-lat1) * (Math.PI/180);
var Δλ = (lon2-lon1) * (Math.PI/180);
//calculate distances
var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
Math.cos(φ1) * Math.cos(φ2) *
Math.sin(Δλ/2) * Math.sin(Δλ/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
//distance
var d = Math.round(R * c);
return d;
}
AFRAME.registerComponent("clickhandler", {
init: function() {
this.el.addEventListener("click", () => {
//alert(this.el.getAttribute('name', 'distance'));
//), (this.el.getAttribute('distance')));
var msg = (
"name: " + (this.el.getAttribute('name'))+
"\ndistance: " + (this.el.getAttribute('distance'))+
"\nbuslines: " + (this.el.getAttribute('buslines'))
);
console.log(msg);
alert(msg);
//alert(this.el.getAttribute("name: "+'name'))//&vbcrlf&"distance:"+'distance'&vbcrlf&"next buslines:"+'buslines'));
//alert(this.el.getAttribute('name'));
});
}
});
/*
/*
var promise = new Promise (function (resolve, reject){
var arr = [];
var id = busstops_callback().lageid;
var sekunden = 300;
$.ajax( { url: "https://rest.busradar.conterra.de/prod/haltestellen"+"/"+id+"/abfahrten?sekunden="+sekunden,
success: getStops
})
}
)
*/
function busstops(id, sekunden){
$.ajax( { url: "https://rest.busradar.conterra.de/prod/haltestellen"+"/"+id+"/abfahrten?sekunden="+sekunden,
success: getStops
})
}
function getStops(x){
var arr = [];
for (i=0; i < x.length; i++){
arr.push(x[i].linienid);
console.log(arr);
return arr;
}
}
function loadPlaces(position){
$.ajax({ url: "https://rest.busradar.conterra.de/prod/haltestellen",
type: "GET",
async: false,
success: busstops_callback
});
var inicall = [];
/**
* @description This function is the callback function of the URL that gets the users closest
* 5 busstops in Muenster, including their distance and their direction and saves all these informations
* in an array.
*/
function busstops_callback(x) {
console.log(x);
//var position = navigator.geolocation.getCurrentPosition();
var narr =[];
//console.log(User_loc);
for (i=0; i< x.features.length; i++ ){
// get name of busstop
var lagebez = x.features[i].properties.lbez;
//get id of busstop
var lageid = x.features[i].properties.nr;
//get coordinates of busstop
var coords = x.features[i].geometry.coordinates;
//get distance of busstop
var distance = dist([position.coords.longitude, position.coords.latitude], coords);
//get buslines within next 5 minutes on that busstop
//var buslines = busstops(lageid, 300);
//save all the data above in a single array
var inarr = [lagebez, coords, distance, lageid];
//push the array into the output array
narr.push(inarr);
//console.log(narr);
}
// sort the busstops in descending disctance order
narr.sort(
function(a,b) {
return a[2] - b[2];
});
// save the closest 5 busstops in the global array
inicall = narr.slice(0,5);
lines(inicall);
//busstops(inicall)
}}
function lines(buslines){
//let o = inicall;
var w = [];
//console.log(busstations)
for (i=0; i<buslines.length; i++){
var busid = buslines[i][3];
let busline = buslines[i];
//console.log(busid);
var url = "https://rest.busradar.conterra.de/prod/haltestellen"+"/"+busid+"/abfahrten?sekunden="+1500;
fetch(url)
.then(function(response){
//console.log(response.text);
return response.json();
})
.then(function(json){
console.log(json.length);
let arr = [];
for (j=0; j < json.length; j++){
arr.push(json[j].linienid);
}
busline.push(arr);
console.log(busline);
})
.catch(function(error){
console.log("Fehler");
})
w.push(busline);
}
console.log(w);
var places = [
{
name: w[0][0],
distance: w [0][2],
buslines: w [0][4],
location: {
lat: w[0][1][1],
lng: w[0][1][0],
}
},
{
name: w[1][0],
distance: w [1][2],
buslines: w [1][4],
location: {
lat: w[1][1][1],
lng: w[1][1][0],
}
},
{
name: w[2][0],
distance: w [2][2],
buslines: w [2][4],
location: {
lat: w[2][1][1],
lng: w[2][1][0],
}
},
{
name: w[3][0],
distance: w [3][2],
buslines: w [3][4],
location: {
lat: w[3][1][1],
lng: w[3][1][0],
}
},
{
name: w[4][0],
distance: w [4][2],
buslines: w [4][4],
location: {
lat: w[4][1][1],
lng: w[4][1][0],
}
}];
console.log(places);
main(places);
}
function main(places){
//places = loadPlaces();
//var places = loadPlaces();
//.then((places) => {
// get scene
var scene = document.querySelector('a-scene');
//return navigator.geolocation.getCurrentPosition(function (position) { //get current user location
// add every Busstop to the scene
places.forEach((place) => {
const latitude = place.location.lat;
const longitude = place.location.lng;
console.log(latitude, longitude);
// add place icon
const icon = document.createElement('a-image');
icon.setAttribute('gps-entity-place', `latitude: ${latitude}; longitude: ${longitude}`);
icon.setAttribute('name', place.name);
icon.setAttribute('distance', place.distance);
icon.setAttribute('buslines', place.buslines);
icon.setAttribute('src', ' ./Bushaltestelle_img.png ');
icon.setAttribute('look-at', '[gps-camera]');
icon.setAttribute('clickhandler', true);
// for debug purposes, just show in a bigger scale
icon.setAttribute('scale', '20, 20');
console.log(icon);
scene.appendChild(icon);
});
(err) => console.error('Error in retrieving position', err),
{
enableHighAccuracy: true,
maximumAge: 0,
timeout: 27000,
}
}
/*
document.querySelector('a-image').addEventListener('click', function (evt) {
alert('This 2D element was clicked!');
});*/