Skip to content

Commit d2e209f

Browse files
committed
fix for threejs location based example
1 parent 4cd29b6 commit d2e209f

File tree

1 file changed

+33
-26
lines changed
  • three.js/examples/location-based

1 file changed

+33
-26
lines changed

three.js/examples/location-based/index.js

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
//import * as THREE from 'three';
2-
// TODO import direct from AR.js npm package
3-
//import * as Arjs from '../js/arjs.js';
1+
function isMobile() {
2+
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
3+
// true for mobile device
4+
return true;
5+
}
6+
return false;
7+
}
48

59
const scene = new THREE.Scene();
610
const camera = new THREE.PerspectiveCamera(80, 2, 0.1, 50000);
@@ -13,7 +17,7 @@ const mesh = new THREE.Mesh(geom, material);
1317
const threex = new THREEx.LocationBased(scene, camera);
1418
const cam = new THREEx.WebcamRenderer(renderer, '#video1');
1519

16-
// If using your own GPS location, change the lon and lat of the three meshes
20+
// If using your own GPS location, change the lon and lat of the four meshes.
1721
threex.add(mesh, -0.72, 51.051); // slightly north
1822
const material2 = new THREE.MeshBasicMaterial({color: 0xffff00});
1923
const material3 = new THREE.MeshBasicMaterial({color: 0x0000ff});
@@ -22,34 +26,37 @@ threex.add(new THREE.Mesh(geom, material2), -0.72, 51.049); // slightly south
2226
threex.add(new THREE.Mesh(geom, material3), -0.722, 51.05); // slightly west
2327
threex.add(new THREE.Mesh(geom, material4), -0.718, 51.05); // slightly east
2428

25-
const get = { m : 0 };
26-
const parts = window.location.href.split('?');
29+
let orientationControls;
2730

28-
if(parts.length==2) {
29-
if(parts[1].endsWith('#')) {
30-
parts[1] = parts[1].slice(0, -1);
31+
if (isMobile()){
32+
orientationControls = new THREEx.DeviceOrientationControls(camera);
33+
if(navigator.geolocation !== 'undefined') {
34+
navigator.geolocation.getCurrentPosition((pos)=>{
35+
if (pos !== 'undefined') {
36+
console.log('geolocation works');
37+
threex.startGps();
38+
} else {
39+
console.log('geolocation error');
40+
}
41+
})
42+
if( navigator.geolocation.getCurrentPosition(()=>{}) == null){
43+
console.log('geolocation not enabled use fakeGps');
44+
threex.fakeGps(-0.72, 51.05);
3145
}
32-
const params = parts[1].split('&');
33-
for(let i=0; i<params.length; i++) {
34-
const param = params[i].split('=');
35-
get[param[0]] = param[1];
46+
47+
} else {
48+
threex.fakeGps(-0.72, 51.05);
3649
}
37-
}
3850

39-
let orientationControls;
40-
41-
// Use query string to control behaviour
42-
// m=1 or m=2, use DeviceOrientationControls (use on mobile device)
43-
// m=2, use actual GPS location
44-
// m not 1, use a fake GPS location
45-
// so m other than 1 or 2 can be used to test on a desktop machine
46-
if(get.m == 1 || get.m == 2) {
47-
orientationControls = new THREEx.DeviceOrientationControls(camera);
48-
}
49-
if(get.m == 2) {
50-
threex.startGps();
51+
threex.on("gpsupdate", pos => {
52+
console.log(`${pos.coords.latitude} ${pos.coords.longitude}`);
53+
});
5154
} else {
5255
threex.fakeGps(-0.72, 51.05);
56+
57+
threex.on("gpsupdate", pos => {
58+
console.log(`${pos.coords.latitude} ${pos.coords.longitude}`);
59+
});
5360
}
5461

5562
requestAnimationFrame(render);

0 commit comments

Comments
 (0)