-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbiergarten.js
121 lines (104 loc) · 4.06 KB
/
biergarten.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
"use strict";
var makeBiergarten = function (pwd) {
var API_KEY = '5MJYmI4aFb4akUO-_G5ew8BPHqlfvc2g_GvOq_nlGPaDwmuM0MUKtrNuCHVmQeKGaNzYSJ-tiwP1UupqIBbIdX6u2wIVw7tjxN2T6TJBRjuzo9fCpH2JJ42sMJl-eKeSduWmckdVCMIiOpxfb7isdPaJRFT4Jd1XSZaRRKPPBx-7skFfhH-6mh0in8rTpbb7FY_Lxld6rCUc2i_oQ9iAJkRC7ZNiNMCgtcKQq_bO1Lx4_Ql4ZhF42d9oYvUhdif4tXpCzJ-mA_hVKTvPuHtxhQFVf9HBSotsM1KOUj8fL0ZXSytk543oMUlpXVdPI5TesmVqMqdih9Z79xvbN1BP1GE28DpkyroAy-Sxij6qvOiL0nQfDVf-tEZjMODdHd1OeQmzsd2KBHCZoZV4tbMu2AmKg9dJSb1nFYpP2342T9t7wtATZ8XAj5_ooImt4_TzHmbWatWGogjcmbVc2AULfiFMpjnI_rkgNVMOkQfO8gk0By1PLIaKawNCsyfekqFvaesLvX0rKW7a-sjoVpm6QxfueG1nv55Svpd0YQZVAjk'
var LOGIN_URI = "http://tourism.opendatahub.bz.it/api/LoginApi?api_key=" + API_KEY;
var BIERGARTEN_URI = "http://tourism.opendatahub.bz.it/api/GastronomyLocalized?api_key" + API_KEY;
var USER_ID = "[email protected]";
var PWD = pwd;
var BEARER_TOKEN = 0;
var gastro_list = [];
function get_token(userid, password, callback) {
// var payload = {
// "username": userid,
// "pswd": password
// };
// BEARER_TOKEN = localStorage.getItem("bearerToken");
// if (BEARER_TOKEN === null)
// {
// $.ajax(
// LOGIN_URI, {
// method: 'POST',
// data: payload,
// success: function (result) {
// set_bearer_token( result);
// callback();
// }
// });
// } else {
// callback();
// }
callback();;
}
function set_bearer_token(token) {
BEARER_TOKEN = token.access_token;
localStorage.setItem("bearerToken", BEARER_TOKEN);
}
function store_biergarten_list( lat, lon, rad) {
return function() {
/* Problem Same Origin Policy
$.ajax(BIERGARTEN_URI+"&pagesize=1024&categorycodefilter=524297&latitude="+ lat + "&longitude=" + lon + "&radius=" + rad, {
headers: { "Authorization": "Bearer " + BEARER_TOKEN},
success: consume_list,
}); */
$.ajax("bier.json", {
success: consume_list,
error: function(a,b,c) {
alert(a);
alert(c);
}});
}
}
function consume_list(result, status) {
alert( result.TotalResults)
console.log(status);
for (var i = 0; i < result.TotalResults; i++) {
var gastro = result.Items[i];
if (gastro !== undefined)
{
console.log(gastro.Latitude, gastro.Longitude, gastro.Shortname)
gastro_list.push( { lat: gastro.Latitude, lon: gastro.Longitude, sn: gastro.Shortname, visited: false });
}
}
}
function coordinateDistance(cord1, cord2) {
// https://www.movable-type.co.uk/scripts/latlong.html
var R = 6371e3; // metres
var φ1 = cord1.latitude * Math.PI / 180;
var φ2 = cord2.latitude* Math.PI / 180;
var Δφ = (cord2.latitude-cord1.latitude) * Math.PI / 180;
var Δλ = (cord2.longitude-cord1.longitude) * Math.PI / 180;
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));
var d = R * c;
return d;
}
var numberOfPois = 0;
var my_callback;
var thus = {
init: function (lat, lon, rad) {
get_token(USER_ID, PWD, store_biergarten_list( lat, lon, rad))
},
subscribe: function(callback) {
my_callback = callback;
},
unsubscribe: function(callback) {
my_callback = undefined;
},
add_position: function( lat, lon) {
for (var i = 0; i < gastro_list.length; i++) {
var d = coordinateDistance( { 'latitude': lat, 'longitude' : lon},
{ 'latitude': gastro_list[i].lat, 'longitude' : gastro_list[i].lon })
if ((d < 100) && (gastro_list[i].visited === false))
{
numberOfPois += 1;
gastro_list[i].visited = true;
var value = { numberOfPois : numberOfPois, lastPoi : gastro_list[i].sn};
my_callback(value);
}
}
}
};
return thus;
}