坐标速度始终返回为'null'



我正在尝试开发一个系统,其中背景上的粒子会根据设备的速度改变其速度(例如,当您在汽车或公共汽车上时(。为此,地理位置API听起来像是一个不错的选择,带有Coordinates.speed属性。

问题是我的速度总是返回为空。从我所读到的内容来看,这应该是一个与设备相关的问题,但是我已经尝试过使用Nexus 6p,Mac,三星平板电脑和iPad,在控制台中始终使用相同的空答案"来自API的速度:空"。

你知道为什么代码不起作用吗?以下是页面和脚本: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>

谢谢!

速度没有在我知道的类似高度的任何浏览器中实现。当您有多个位置时,您当然可以启发式地估计速度,但识别灯光等处的停止并不容易或准确。

有关完整示例,请参阅Brotkrumen。

最新更新