如何在Cordova Android平台上获得绝对指南针方向 [英] How to get absolute compass heading on Cordova Android platform

查看:103
本文介绍了如何在Cordova Android平台上获得绝对指南针方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Android平台上使用Cordova开发混合应用程序.我想使指南针在设备方向和真实北向之间移动.

I am developping an hybrid App using Cordova on Android platform. I would like to get the compass heading between my device orientation and the true North.

但是"deviceorientationabsolute"事件(如此处 https所示)://developers.google.com/web/updates/2016/03/device-orientation-changes )目前为我提供了相对于设备初始方向的指南针值.我期望参考真正的北方是一个绝对的价值.

However the 'deviceorientationabsolute' event (as documented here https://developers.google.com/web/updates/2016/03/device-orientation-changes) currently gives me the compass value relative to the initial orientation of my device. I was expecting an absolute value in reference to the true North.

使用chrome控制台,我调试了设备并检查'deviceorientationabsolute'事件是否可用

Using chrome console, I debugged my device and checked that the 'deviceorientationabsolute' event is available

('ondeviceorientationabsolute' in window)
true

我还询问了网络视图使用的是哪个版本的导航器:

I also asked what version of navigator is being used for the webview:

navigator.userAgent

"Mozilla/5.0 (Linux; Android 8.1.0; Android SDK built for x86 Build/OSM1.180201.031; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/69.0.3497.100 Mobile Safari/537.36"

这里是Cordova版本(在项目文件夹中)

Here is for Cordova version (in project folder)

cordova --version
9.0.0 (cordova-lib@9.0.1)

这是我用来获取指南针标题的代码

Here is the code I am using to get the compass heading

window.addEventListener("deviceorientationabsolute", getorientation, true);

function getorientation(event) {
    compassbearing = Number(360 - event.alpha).toFixed(1);
    $("#InputCompassbearing").val(compassbearing);
}

推荐答案

此行为显然是由于设备上的罗盘校准问题引起的.校准指南针后,它可以正常工作.

This behaviour was apparently caused by a problem of compass calibration on my device. After calibrating the compass, it worked fine.

让我困扰的是未触发需要校准"事件.相反,它给了我指南针相对值,这可能是Chrome或Android API的不当行为.

What troubles me is that the event "needs calibration" was not fired. It gave me the compass relative value instead which is probably a misbehaviour from the Chrome or Android API.

这篇关于如何在Cordova Android平台上获得绝对指南针方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆