Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing iPhone compass with JavaScript

Know if it's possible to access the iPhone compass in Safari using JavaScript? I see how the GPS can be accessed, but I can't figure out the compass.

like image 591
Drew Dara-Abrams Avatar asked Apr 01 '10 23:04

Drew Dara-Abrams


People also ask

How do I access my iPhone Compass?

Go to Settings > Privacy & Security > Location Services, then turn on Location Services. Tap Compass, then tap While Using the App.

How do I get the True North Compass on my iPhone?

Once the compass has been calibrated, the compass at 0 degrees will point to magnetic north — this differs from True North. If you want your iPhone's compass to always point to True North, you can change it by going to Settings > Compass > "Use True North."

How accurate are coordinates on iPhone Compass?

With all location services enabled, the "compass" app only shows location (latitude, longitude) to the nearest arcsecond, which is about 100 feet.


2 Answers

On iOS, you can retrieve the compass value like this.

window.addEventListener('deviceorientation', function(e) {
    console.log( e.webkitCompassHeading );
}, false);

For more informations, read the Apple DeviceOrientationEvent documentation.

Hope this helps.

like image 179
Simon Arnold Avatar answered Oct 04 '22 15:10

Simon Arnold


You cannot access that information via javascript, unless you're using something like iPhoneGap

At the time this was true, in iOS 5 you can use the compass heading in JS. https://developer.apple.com/documentation/webkitjs/deviceorientationevent/1804777-webkitcompassheading

like image 41
christo16 Avatar answered Oct 04 '22 13:10

christo16