This repository has been archived by the owner on Jan 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Compass.js.html
334 lines (297 loc) · 8.43 KB
/
Compass.js.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: Compass.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: Compass.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>// https://aviation.stackexchange.com/questions/8000/what-are-the-differences-between-bearing-vs-course-vs-direction-vs-heading-vs-tr
class Compass {
/**
* The compass class is a utility for creating a javascript compass
* with no external dependencies. You can create a compass that
* points north from your location or points towards a specified
* latitude and longitude coordinate. Regardless of where you turn
* the compass will point towards the specified direction.
*
* @class
*/
constructor() {
this.heading = 0;
this.deviceAngleDelta = 0;
this.position = null;
this.geolocationID = null;
this.permissionGranted = false;
this.debug = false;
}
/**
* initialized the compass - returns a promise or can invoke a callback
* @param {callback} callback - callback to be called after the .start() function is done
*/
init(callback = undefined) {
if (callback) {
this.callCallback(this.start(), callback);
} else {
return this.start();
}
}
/**
* Initializes the device orientation and watches the user position by default
* @async
* @function start
*
*/
async start() {
try {
await this.watchPosition();
const confirmation = await this.confirmDialog(
"allow compass to access device orientation?"
);
if (confirmation) {
await this.allowOrientationPermissions();
} else {
alert("compass permissions were not granted");
}
} catch (err) {
alert(err);
}
}
/**
* Promisifies the confirm dialog
*
* @param {String} msg
*/
confirmDialog(msg) {
return new Promise(function (resolve, reject) {
let confirmed = window.confirm(msg);
return confirmed ? resolve(true) : reject(false);
});
}
/**
* Asks the user to allow permissions to get orientation
*
* @async
* @name allowOrientationPermissions
*/
async allowOrientationPermissions() {
if (typeof window.DeviceOrientationEvent.requestPermission === "function") {
const permission = await window.DeviceOrientationEvent.requestPermission();
alert(permission);
if (permission == "granted") {
window.addEventListener(
"deviceorientation",
this.deviceOrientationHandler.bind(this),
true
);
return true;
} else {
throw new Error("no device orientation permissions!");
}
} else {
if (window.DeviceOrientationEvent) {
window.addEventListener(
"deviceorientation",
this.deviceOrientationHandler.bind(this),
true
);
} else {
alert("no device orientation support");
}
}
}
/**
* This is where my nose points - and seeing as my nose
* is attached to my head, this is where my head
* (and thus my machine) is pointing relative to North.
* NOTE: requires that this.position is set
*
* @function getHeading
*/
getHeading(
origin = {
lat: this.position.coords.latitude,
lng: this.position.coords.longitude,
},
north = { lat: 90, lng: this.position.coords.longitude }
) {
this.heading = 360 - this.getBearingToNorth(origin, north);
return this.heading;
}
/**
* This is the angle between the location of an object,
* machine or destination and my heading.
*
* @param {Object} origin - {lat, lng}
* @param {Object} destination - {lat, lng}
*/
getBearing(origin, destination) {
return (
this.calculateAngle(
origin.lat,
origin.lng,
destination.lat,
destination.lng
) *
(180 / Math.PI)
);
}
/**
* get the angle between your heading and north
* the default is true north vs. magnetic north
*
* @function
* @param {object} origin - {lat, lng}
* @param {object} north - {lat, lng}
*/
getBearingToNorth(
origin = {
lat: this.position.coords.latitude,
lng: this.position.coords.longitude,
},
north = { lat: 90, lng: this.position.coords.longitude }
) {
return this.getBearingToDestination(origin, north);
}
/**
* Get the bearings towards the destination
*
* @param {object} origin - {lat, lng}
* @param {object} destination - {lat, lng}
*/
getBearingToDestination(
origin = {
lat: this.position.coords.latitude,
lng: this.position.coords.longitude,
},
destination
) {
const angleToDestination = this.getBearing(origin, destination);
return this.deviceAngleDelta + angleToDestination;
}
/**
* Handles changes created by the device orientation changes
* assumes that the phone is in a portrait mode, with the display up
* towards the sky as if you were holding an actual compass
*
* @callback
* @param {object} evt - the event object of the device orientation
*/
deviceOrientationHandler(evt) {
if (evt.webkitCompassHeading)
//iphone
this.deviceAngleDelta = 360 - evt.webkitCompassHeading;
else if (evt.alpha)
//android
this.deviceAngleDelta = evt.alpha;
else {
console.log("compass direction not found");
}
// console.log(this.deviceAngleDelta)
this.deviceAngleDelta = Math.round(this.deviceAngleDelta);
}
/**
* get the position of the user
*
* @async
* @function
*/
getPosition() {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition((position) => {
if (position) {
resolve(position);
} else {
reject("no position found");
}
});
});
}
/**
* watches the geolocation of the user
*
* @async
* @function
*/
watchPosition() {
return new Promise((resolve, reject) => {
this.geolocationID = navigator.geolocation.watchPosition((position) => {
if (position) {
this.position = position;
console.log(
this.position.coords.latitude,
this.position.coords.longitude
);
resolve(position);
} else {
reject("no position found");
}
});
});
}
/**
* Stops watching the user location
*
* @function
*/
stopTracking() {
navigator.geolocation.clearWatch(this.geolocationID);
console.log("stopped tracking location");
}
/**
* Calculates the angle given a latitude and longitude position
*
* @function
* @param {number} userLat - user latitude
* @param {number} userLon - user longitude
* @param {number} desiredLat - desired latitude
* @param {number} desiredLon - desired longitude
*/
calculateAngle(userLat, userLon, desiredLat, desiredLon) {
return Math.atan2(desiredLon - userLon, desiredLat - userLat);
}
/**
* Helper function that allows calling a callback from an promise function
*
* @param {promise} promise
* @param {callback} callback
*/
callCallback(promise, callback) {
if (callback) {
promise
.then((result) => {
callback(undefined, result);
return result;
})
.catch((error) => {
callback(error);
return error;
});
}
return promise;
}
}</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Compass.html">Compass</a></li></ul><h3>Global</h3><ul><li><a href="global.html#allowOrientationPermissions">allowOrientationPermissions</a></li><li><a href="global.html#getHeading">getHeading</a></li><li><a href="global.html#start">start</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.4</a> on Sat Apr 25 2020 01:37:55 GMT-0400 (Eastern Daylight Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>