accessing iPhone compass with JavaScript

2019-02-17 01:56发布

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.

Thanks, as always!

3条回答
forever°为你锁心
2楼-- · 2019-02-17 02:30

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.

查看更多
【Aperson】
3楼-- · 2019-02-17 02:40
我只想做你的唯一
4楼-- · 2019-02-17 02:40

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

very simple to use with events and methods.

You can try a demo here:
http://labs.easyblog.it/maps/leaflet-compass/

查看更多
登录 后发表回答