-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgps-entity-place.js
140 lines (114 loc) · 4.37 KB
/
gps-entity-place.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
AFRAME.registerComponent('gps-entity-place', {
_cameraGps: null,
schema: {
longitude: {
type: 'number',
default: 0,
},
latitude: {
type: 'number',
default: 0,
}
},
init: function () {
window.addEventListener('gps-camera-origin-coord-set', function () {
if (!this._cameraGps) {
var camera = document.querySelector('[gps-camera]');
if (!camera.components['gps-camera']) {
console.error('gps-camera not initialized')
return;
}
this._cameraGps = camera.components['gps-camera'];
}
this._updatePosition();
}.bind(this));
window.addEventListener('gps-camera-update-position', function (ev) {
if (!this.data || !this._cameraGps) {
return;
}
var dstCoords = {
longitude: this.data.longitude,
latitude: this.data.latitude,
};
// it's actually a 'distance place', but we don't call it with last param, because we want to retrieve distance even if it's < minDistance property
var distance = this._cameraGps.computeDistanceMeters(ev.detail.position, dstCoords);
this.el.setAttribute('distance', distance);
this.el.setAttribute('distanceMsg', formatDistance(distance));
this.el.dispatchEvent(new CustomEvent('gps-entity-place-update-positon', { detail: { distance: distance } }));
}.bind(this));
this._positionXDebug = 0;
window.dispatchEvent(new CustomEvent('gps-entity-place-added'));
console.debug('gps-entity-place-added');
this.debugUIAddedHandler = function () {
this.setDebugData(this.el);
window.removeEventListener('debug-ui-added', this.debugUIAddedHandler.bind(this));
};
window.addEventListener('debug-ui-added', this.debugUIAddedHandler.bind(this));
},
/**
* Update place position
* @returns {void}
*/
_updatePosition: function () {
var position = { x: 0, y: this.el.getAttribute('position').y || 0, z: 0 }
var hideEntity = false;
// update position.x
var dstCoords = {
longitude: this.data.longitude,
latitude: this._cameraGps.originCoords.latitude,
};
position.x = this._cameraGps.computeDistanceMeters(this._cameraGps.originCoords, dstCoords, true);
// place has to be hide
if (position.x === Number.MAX_SAFE_INTEGER) {
hideEntity = true;
}
this._positionXDebug = position.x;
position.x *= this.data.longitude > this._cameraGps.originCoords.longitude ? 1 : -1;
// update position.z
var dstCoords = {
longitude: this._cameraGps.originCoords.longitude,
latitude: this.data.latitude,
};
position.z = this._cameraGps.computeDistanceMeters(this._cameraGps.originCoords, dstCoords, true);
// place has to be hide
if (position.z === Number.MAX_SAFE_INTEGER) {
hideEntity = true;
}
position.z *= this.data.latitude > this._cameraGps.originCoords.latitude ? -1 : 1;
if (position.y !== 0) {
position.y = position.y - this._cameraGps.originCoords.altitude;
}
if (hideEntity) {
this.el.setAttribute('visible', false);
} else {
this.el.setAttribute('visible', true);
}
// update element's position in 3D world
this.el.setAttribute('position', position);
},
/**
* Set places distances from user on debug UI
* @returns {void}
*/
setDebugData: function (element) {
var elements = document.querySelectorAll('.debug-distance');
elements.forEach(function (el) {
var distance = formatDistance(this._positionXDebug);
if (element.getAttribute('value') == el.getAttribute('value')) {
el.innerHTML = el.getAttribute('value') + ': ' + distance + 'far';
}
});
},
});
/**
* Format distances string
*
* @param {String} distance
*/
function formatDistance(distance) {
distance = distance.toFixed(0);
if (distance >= 1000) {
return (distance / 1000) + ' kilometers';
}
return distance + ' meters';
};