Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the Magnetometer from this Sensor API?

I'm trying to get use the magnetometer from this Sensor API but I'm not sure if I'm doing so correctly.

I copied and edited the example code from the their site into my test 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);

But when I load the page it doesn't give me any readings. Checking the site on my laptop brings back this error message;

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

Any insight into this would be greatly appreciated.

like image 630
Nic Hooper Avatar asked Oct 29 '22 03:10

Nic Hooper


1 Answers

I found the answer. After looking around I found that you need to go to chrome://flags/#enable-generic-sensor-extra-classes and enable Generic Sensor Extra Classes.

I'm not sure why this is the case but I am now getting the readings I was after.

like image 145
Nic Hooper Avatar answered Nov 15 '22 06:11

Nic Hooper