Access iPhone compass using JavaScript

Do you know to access your iPhone compass in Safari using JavaScript? I see how you can access the GPS , but I can not understand the compass.

+7
iphone compass-geolocation
source share
3 answers

You cannot access this information via JavaScript unless you use something like iPhoneGap

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

+3
source share

In iOS, you can get a compass value like this.

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

Read the Apple DeviceOrientationEvent documentation for more information.

Hope this helps.

+3
source share

I advise you to use LeafletJS with this plugin
https://github.com/stefanocudini/leaflet-compass

very easy to use with events and methods.

Here you can try the demo:
http://labs.easyblog.it/maps/leaflet-compass/

0
source share

All Articles