-
Notifications
You must be signed in to change notification settings - Fork 4
/
gps-tracker.html
449 lines (414 loc) · 17.2 KB
/
gps-tracker.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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<html lang="en">
<head>
<meta charset="utf-8">
<title> GPS Tracker </title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="./css/style.css">
<script type="text/javascript" src="https://beta.altizure.com/sdk"></script>
<script type="text/javascript" src="./js/timeline.js"></script>
</head>
<body>
<div id="root">
<div class="hud">
<div class="component">
<div class="componentLabel">Tracker</div>
<button class="button button1" id="startTrack">Start</button>
<div class="componentLabel">Waypoint #:</div>
<div class="componentText" id="waypointTrack"> N/A </div>
<button class="button button1" id="clearTrack">Clear</button>
<div class="componentLabel">2D Track</div>
<label class="switch">
<input type="checkbox" id="track2DSwitch">
<span class="switchSlider"></span>
</label>
</div>
<div class="component">
<div class="componentLabel">Last waypoint:</div>
<div class="componentText" id="lastWaypoint"> N/A; N/A; N/A </div>
</div>
<div class="component">
<div class="componentLabel">Camera</div>
<button class="button button1" id="orbitbutton">Orbit</button>
<div class="componentLabel">Follow Me</div>
<label class="switch">
<input type="checkbox" id="followSwitch">
<span class="switchSlider"></span>
</label>
</div>
<div class="component">
<div class="componentLabel">Hint:</div>
<div class="componentText" id="hintText"> Press Start to track your position </div>
</div>
</div>
<canvas id="overlayCanvas2d"></canvas>
<div id="page-content"></div>
</div>
<script>
// check whether the geo location is enabled
function checkGeoLocation () {
navigator.geolocation.getCurrentPosition(onPageLoaded,
(error) => {
alert('You must enable geolocation to run this demo')
alert(error)
console.log(error)
}, {
enableHighAccuracy: true,
timeout: 10000,
maximumAge: 0
})
}
function onPageLoaded (startPos) {
console.log('start position:', startPos)
let projects = [
{pid: '58428f1d97b73e0b090675cd', text: ''}
]
// 22.335975, 114.263718 // on google map
// 22.335392, 114.262712 // on altizure ust model without pose
// 22.334281, 114.263475 // on altizure ust model with pose as 22.335392, 114.262712
// the altizure model should offset
// 0.001694 0.000243
// new pose is
// 22.335392, 114.262712 - 0.001694 0.000243
// = 22.333698 114.262469
const center = {
lat:22.335975,
lng:114.263718,
alt:1020
}
let options = {
altizureApi:{
key: '7MkQf8UggsPaadvrlKALspJWZejZAJOLHn3cnIy'
},
camera: {
poseTo: { alt:1200,
lat: center.lat,
lng: center.lng
},
flyTo: { alt: center.alt,
lat: center.lat,
lng: center.lng,
north: -27.271585092584015,
tilt: 74.7023627906978
}
},
renderItems: {
earth: true,
earthUseTexture: false,
featureInView: false,
orbitRing: false
}
}
let sandbox = new altizure.Sandbox('page-content', options)
let pagecontent = document.getElementById("page-content")
// camera orbit animation
let orbitButton = document.getElementById("orbitbutton")
let orbitAnimation = null
orbitButton.innerHTML = 'Orbit'
orbitButton.addEventListener("click", () => {
if (orbitButton.innerHTML === 'Orbit') {
if (orbitAnimation) {
clearInterval(orbitAnimation)
}
orbitAnimation = setInterval(function() {
let currentPos = sandbox.camera.pose
currentPos.north += 10
sandbox.camera.pose = currentPos
}, 500)
orbitButton.innerHTML = 'Stop'
} else {
if (orbitAnimation) {
clearInterval(orbitAnimation)
}
orbitButton.innerHTML = 'Orbit'
}
})
// load the project
timelineLayout(sandbox, projects, {
cameraPos: {
lat: 22.334522,
lng: 114.263429,
alt: 1020
},
projOffset: {
lng: 0,
lat: 0
},
labelOffset: {
lng: 0,
lat: -0.01
},
defaultVisible: true,
title: '香港科技大学'
}).then((entities) => {
//////////////////////////
// After the scene is loaded, start to initialize all other event handlers
//////////////////////////
let mainProj = entities['0'].proj
mainProj.water.import()
// UI stuffs
let followSwitch = document.getElementById('followSwitch')
let startTrackButton = document.getElementById("startTrack")
let trackText = document.getElementById("waypointTrack")
let lastWaypointText = document.getElementById("lastWaypoint")
let clearTrackButton = document.getElementById('clearTrack')
let track2DSwitch = document.getElementById('track2DSwitch')
let canvas = document.getElementById('overlayCanvas2d')
let hintText = document.getElementById('hintText')
// Const variables
const altOffset = 10
const geoOptions = {
enableHighAccuracy: true,
timeout: 10000,
maximumAge: 0
}
// State variables
let draw2DTrack = false
let followCamera = false
// Core data
let trackedWayPoints = []
// 3D content
// add polyline marker
let trackMarker = null
// insert a user tag
let userTag = null
// Setup track style swtich
track2DSwitch.onchange = function () {
draw2DTrack = this.checked
if (trackMarker) {
trackMarker.visible = !draw2DTrack
}
updateCanvas2d()
}
// Setup camera follow switch
followSwitch.onchange = function () {
followCamera = this.checked
}
// setup canvas for 2d lines
// set the canvas size correctly
canvas.setAttribute('width', canvas.clientWidth)
canvas.setAttribute('height', canvas.clientHeight)
window.addEventListener('resize', function(){
canvas.setAttribute('width', canvas.clientWidth)
canvas.setAttribute('height', canvas.clientHeight)
}, true)
let ctx2d = canvas.getContext('2d')
function updateCanvas2d () {
if (trackedWayPoints && trackedWayPoints.length >= 2) {
// When there are more than 1 point, project the points and draw on canvas
ctx2d.clearRect(0, 0, canvas.width, canvas.height)
if (!draw2DTrack) {
return
}
const offsetPt = {
lng: center.lng + trackedWayPoints[0].position.lng - trackedWayPoints[0].position.lng,
lat: center.lat + trackedWayPoints[0].position.lat - trackedWayPoints[0].position.lat,
alt: trackedWayPoints[0].position.alt
}
const scrPt = sandbox.window.fromLngLatAlt(offsetPt)
ctx2d.beginPath()
ctx2d.strokeStyle = "#0022EE"
ctx2d.lineWidth = "3"
ctx2d.moveTo(scrPt.x, scrPt.y)
for (let i = 1; i < trackedWayPoints.length; ++i) {
const pt = trackedWayPoints[i]
const offsetPt = {
lng: center.lng + pt.position.lng - trackedWayPoints[0].position.lng,
lat: center.lat + pt.position.lat - trackedWayPoints[0].position.lat,
alt: pt.position.alt
}
const scrPt = sandbox.window.fromLngLatAlt(offsetPt)
ctx2d.lineTo(scrPt.x, scrPt.y)
}
ctx2d.stroke()
}
}
// update the canvas size when the window is resized.
sandbox.on('cameraChange', updateCanvas2d)
// set start record mode
let trackTimer = null
startTrackButton.addEventListener('click', (e) => {
if (startTrackButton.innerHTML === 'Start') {
startTrackButton.innerHTML = 'Stop'
if (!trackTimer) {
navigator.geolocation.clearWatch(trackTimer)
trackTimer = null
}
trackTimer = navigator.geolocation.watchPosition(
(pos) => {
let depthAlt = 0
console.log(depthAlt)
// insert the waypoints
if (trackedWayPoints.length === 0) {
depthAlt = mainProj.pickDepthMap([{
lat: pos.coords.latitude,
lng: pos.coords.longitude,
alt: altOffset
}])[0]
// reset the center.lat/lng if the point is inside this data
// 22.344809, 114.272569 ust top right
// 22.324247, 114.254293 ust bottom left
if (pos.coords.latitude >= 22.324247 && pos.coords.latitude <= 22.344809
&& pos.coords.longitude >= 114.254293 && pos.coords.longitude <= 114.272569) {
center.lat = pos.coords.latitude
center.lng = pos.coords.longitude
hintText.innerHTML = 'Press stop to stop tracking.'
} else {
hintText.innerHTML = 'Your current location is too far from this model. The starting point will be set to the center of this model.'
depthAlt = mainProj.pickDepthMap([{
lat: center.lat,
lng: center.lng,
alt: altOffset
}])[0]
}
// initialize the list of waypoints
trackedWayPoints.push({
position: {
lat: pos.coords.latitude,
lng: pos.coords.longitude,
alt: depthAlt
},
orientation: {
northing: 0
},
timeStamp: pos.timestamp
})
if (userTag) {
userTag.destruct()
userTag = null
}
if (!userTag) {
userTag = new altizure.TagMarker({
imgUrl: './images/altizure-logo.png',
// icon position
position: {
lat: pos.coords.latitude,
lng: pos.coords.longitude,
alt: depthAlt
},
// scene
sandbox: sandbox,
visible: true,
scale: 5 // icon size
})
}
} else {
depthAlt = mainProj.pickDepthMap([{
lat: center.lat + pos.coords.latitude - trackedWayPoints[0].position.lat,
lng: center.lng + pos.coords.longitude - trackedWayPoints[0].position.lng,
alt: altOffset
}])[0]
const distance = greatCircleDistance(
trackedWayPoints[trackedWayPoints.length-1].position.lat,
trackedWayPoints[trackedWayPoints.length-1].position.lng,
pos.coords.latitude,
pos.coords.longitude
)
if (distance > 4) {
// insert new way point, if the user moves more than 4 meters
// let depthAlt = mainProj.pickDepthMap([{
// lat: pos.coords.latitude,
// lng: pos.coords.longitude,
// alt: altOffset
// }])[0]
trackedWayPoints.push({
position: {
lat: pos.coords.latitude,
lng: pos.coords.longitude,
alt: depthAlt
},
orientation: {
northing: 0
},
timeStamp: pos.timestamp
})
if (trackMarker) {
// if the marker for waypoints exists, add a new point to the end
// and update canvas if needed.
const pt = trackedWayPoints[trackedWayPoints.length - 1]
trackMarker.addPoint(new altizure.LngLatAlt(
center.lng + pt.position.lng - trackedWayPoints[0].position.lng,
center.lat + pt.position.lat - trackedWayPoints[0].position.lat,
pt.position.alt
))
updateCanvas2d()
}
}
}
// New a polyline marker to represent the track if there is such a marker
if (trackedWayPoints.length >= 2 && !trackMarker) {
let points = trackedWayPoints.map((pt) => {
return new altizure.LngLatAlt(
center.lng + pt.position.lng - trackedWayPoints[0].position.lng,
center.lat + pt.position.lat - trackedWayPoints[0].position.lat,
pt.position.alt)
})
// create the track marker if the waypoints is >= 2
trackMarker = new altizure.PolyCylinderLineMarker({
name: 'tracks',
sandbox: sandbox,
points: points,
color: 0xb73026, // line color
visible: !draw2DTrack,
lineWidth: 3 // line height (for visualization)
})
}
updateCanvas2d()
// update userTag position, userTag and text are updated everytime pos is updated
userTag.setPose({
lat: center.lat + pos.coords.latitude - trackedWayPoints[0].position.lat,
lng: center.lng + pos.coords.longitude - trackedWayPoints[0].position.lng,
alt: depthAlt
},
undefined,
undefined
)
// update the camera pose if the user enables camera-follow feature.
if (followCamera) {
let currentPos = sandbox.camera.pose
currentPos.lat = center.lat + pos.coords.latitude - trackedWayPoints[0].position.lat
currentPos.lng = center.lng + pos.coords.longitude - trackedWayPoints[0].position.lng
sandbox.camera.pose = currentPos
}
// update text for last waypoint and waypoint number
lastWaypointText.innerHTML = '' + (center.lat + pos.coords.latitude - trackedWayPoints[0].position.lat).toPrecision(9) + '; ' + (center.lng + pos.coords.longitude - trackedWayPoints[0].position.lng).toPrecision(9) + '; ' + depthAlt.toPrecision(9)
trackText.innerHTML = '' + trackedWayPoints.length
},
(error) => {
console.log(error)
alert(error)
lastWaypointText.innerHTML = 'Error in location change'
},
geoOptions
)
} else {
startTrackButton.innerHTML = 'Start'
if (trackTimer) {
navigator.geolocation.clearWatch(trackTimer)
trackTimer = null
lastWaypointText.innerHTML = 'N/A; N/A; N/A'
trackText.innerHTML = '' + trackedWayPoints.length
} else {
console.log('Empty tracker')
}
}
})
// Clear the tracked points
clearTrackButton.addEventListener('click', (e) => {
trackedWayPoints = []
lastWaypointText.innerHTML = 'N/A; N/A; N/A'
trackText.innerHTML = '' + trackedWayPoints.length
if (trackMarker) {
trackMarker.destruct()
trackMarker = null
}
if (userTag) {
userTag.destruct()
userTag = null
}
})
})
}
document.addEventListener("DOMContentLoaded", checkGeoLocation)
</script>
</body>
</html>