DeviceOrientation Compass Android

问题描述 投票:0回答:1

我尝试使用deviceorientation事件实现指南针。我想使用旋转方向来旋转朝着查看方向的箭头。当deviceorientation更改时,我只是旋转图像。

在iOS上,通过执行以下操作,就像魅力一样工作:

if (typeof DeviceOrientationEvent.requestPermission === "function") {
          //@ts-ignore
          DeviceOrientationEvent.requestPermission()
            .then(permissionState => {
              if (permissionState === "granted") {
                this.compassActive = true;
                window.addEventListener(
                  "deviceorientation",
                  eventData =>
                    this.zone.run(() => {
                      if(!this.compassActive){
                        return false;
                      }
                      //@ts-ignore
                      this.rotatePlayerIcon(eventData.webkitCompassHeading);

                    }),
                  false
                );
              }
            })
            .catch(console.error);

由于DeviceOrientationEvent.webkitCompassHeading为我提供了设备旋转的基于顺时针世界的显示。

我在Android上尝试相同,但是找不到基于世界的解决方案。 webkitCompassHeading在Android上不存在,因此我尝试仅使用eventData.alpha。但这会根据事件触发时的旋转次数(而不是基于世界北部)得出0。

我发现的所有指南似乎都已过时。

如何像在iOS上一样在Android上获得顺时针罗盘?

javascript android html compass device-orientation
1个回答
1
投票

这是我基于vtm-mapsforge指南针创建的指南针类。它还包括一个箭头。我还使用位置感应,因为传感器根本不准确,很容易被破坏。指南针的好处是它包含代码,可以使传感器平稳地旋转,作为传感器的原始输出通常很吵。

我目前正在为您编辑代码,但同时查看this,它是vtm-mapsforge罗盘的原始版本

© www.soinside.com 2019 - 2024. All rights reserved.