0
votes

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!

1
Don't put links to your code. Instead, you should paste the code in your question in a minimal reproducible exampleSandra K
When you test this on your various devices, i'm guessing it's through the www (rather than localhost or file://). Unless it's https/tls, or localhost or file://, it's not a secure context.,which is a necessity for accessing many privacy-sensitive APIs, such as your Coordinates.speed property.cptwonton
@cptwonton I am testing on a secure context with https, since I thought the original problem was testing through localhost, but it doesn't change anything regarding the Coordinates.speed property.Marcelo Scharlau Coelho
@SandraK thank you for the suggestion, I hope it's better now!Marcelo Scharlau Coelho

1 Answers

0
votes

Speed is not implemented in any browser that I am aware of similarly altitude. When yo have more than one position you can of course heuristically estimate speed but identifying a stop at the lights etc is not easy or accurate.

See Brotkrumen for a complete example