-
Notifications
You must be signed in to change notification settings - Fork 1
/
ThunkItHaversine.html
140 lines (104 loc) · 3.51 KB
/
ThunkItHaversine.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
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="">
<script src="https://thunkable.github.io/webviewer-extension/thunkableWebviewerExtension.js"
type="text/javascript"></script>
<style>
span {
font-size: 10px;
}
</style>
</head>
<body>
<ul id="places-list"></ul>
<span id="result">results here</span>
<script>
function toRad(v) { return v * Math.PI / 180; }
function kmToMiles(km) { return (km * 0.62137).toFixed(2); }
var l2 = LatLong(-85, 42);
function LatLong(lat, lon) {
return { Latitude: lat, Longitude: lon }
}
function haversine(l1, l2) {
var R = 6371; /*km*/
var x1 = l2.Latitude - l1.location[0];
var dLat = toRad(x1);
var x2 = l2.Longitude - l1.location[1];
var dLon = toRad(x2);
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(toRad(l1.location[0])) * Math.cos(toRad(l2.Latitude)) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c;
return d;
}
ThunkableWebviewerExtension.receiveMessage(function (message) {
//ThunkableWebviewerExtension.postMessage(message);
var messageList = JSON.parse(message);
var index = messageList.index
var data = messageList.Locations
var whatisit = typeof data;
var returnObjects = [];
data.forEach(element => {
if (kmToMiles(haversine(element, l2)) > 10) {
returnObjects.push(element.City + ' is ' + kmToMiles(haversine(element, l2)) + ' f away');
}
});
document.getElementById("result").innerHTML = returnObjects;
ThunkableWebviewerExtension.postMessage(returnObjects);
//parse string from app into JSON object
//setTimeout(function () {
// }, 250);
});
function renderPlaces(doc){
//create the visual components
let li = document.createElement('li');;
let place = document.createElement('span');
let city = document.createElement('span');
let interestingFact = document.createElement('span');
let cross = document.createElement('div');
let button = document.createElement('button')
let input = document.createElement('input')
//set the visual properties
li.setAttribute('data-id', doc.id);
place.textContent = doc.data().Place;
city.textContent = doc.data().City;
interestingFact.textContent = doc.data().interestingFact;
cross.textContent = 'x';
button.textContent = 'edit';
input.placeholder = 'typeshithere'
input.setAttribute("id", doc.id)
let inputID = doc.id;
//add the items set above to the list item 'li'
li.appendChild(place)
li.appendChild(city)
li.appendChild(interestingFact)
li.appendChild(cross)
li.appendChild(button)
li.appendChild(input)
//append the list item to the list of list items
placesList.appendChild(li)
//deletingPlaces added to each list
cross.addEventListener('click', (e) =>{
e.stopPropagation();
let id = e.target.parentElement.getAttribute('data-id');
db.collection('places').doc(id).delete();
})
//update a place 'interestingFact'
button.addEventListener('click', (e) => {
e.stopPropagation();
let id = e.target.parentElement.getAttribute('data-id');
db.collection('places').doc(id).update({
interestingFact: document.getElementById(inputID).value
})
document.getElementById(inputID).value = ""
})
}
</script>
</body>
</html>