【问题标题】:How can I use the Magnetometer from this Sensor API?如何使用此传感器 API 中的磁力计?
【发布时间】:2018-10-11 10:35:40
【问题描述】:

我正在尝试通过此 Sensor API 使用磁力计,但我不确定我是否正确。

我将their site 中的示例代码复制并编辑到我的测试站点中;

let sensor = new Magnetometer();
sensor.start();

sensor.onreading = () => {
    console.log("Magnetic field along the X-axis " + sensor.x);
    console.log("Magnetic field along the Y-axis " + sensor.y);
    console.log("Magnetic field along the Z-axis " + sensor.z);

    document.getElementById("x").innerHTML = "X = " + sensor.x;
    document.getElementById("y").innerHTML = "Y = " + sensor.y;
    document.getElementById("z").innerHTML = "Z = " + sensor.z;
};

sensor.onerror = event => console.log(event.error.name, event.error.message);

但是当我加载页面时,它没有给我任何读数。在我的笔记本电脑上检查该站点会返回此错误消息;

Uncaught ReferenceError: Magnetometer is not defined
    at magnetometer.js:1

任何对此的见解将不胜感激。

【问题讨论】:

    标签: android google-chrome magnetometer


    【解决方案1】:

    我找到了答案。看了一圈发现需要去chrome://flags/#enable-generic-sensor-extra-classes开启Generic Sensor Extra Classes

    我不确定为什么会这样,但我现在得到了我想要的读数。

    【讨论】:

    • Chrome 76 仍需要
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-05
    • 1970-01-01
    相关资源
    最近更新 更多