I am trying to develop a system where the particles on the background change their speed accordingly to the speed of the device (while you are in a car or bus, for example). For this, the Geolocation API sounded like a great option, with the Coordinates.speed property.
The problem is that my speed is always returning as null. From what I've read, it should be a device-related issue, but I have tried with a Nexus 6p, a Mac, a Samsung tablet and an iPad, always with the same null answer in the console "speed from API: null".
Have you got any idea of why the code is not working? Following is the page and the script: https://marceloscoelho.github.io
(...)
<script>
//getting the user location
var userSpeed = null;
function getLocation() {
setInterval(function(){
navigator.geolocation.watchPosition(showPosition); }, 500);
}
function render() {
if (userSpeed == null) {
userSpeed = 0.00005;
}
console.log("Speed used :" + userSpeed);
percentage += userSpeed; //Speed of the tunnel
var p1 = path.getPointAt(percentage % 1);
var p2 = path.getPointAt((percentage + 0.01) % 1);
camera.position.set(p1.x, p1.y, p1.z);
camera.lookAt(p2);
//Render the scene
renderer.render(scene, camera);
requestAnimationFrame(render);
}
requestAnimationFrame(render);
function showPosition(position){
var speed = position.coords.speed;
console.log("Speed from API: " + speed);
// if too big
speed = speed / 1000000;
userSpeed = speed;
}
</script>
Thank you!