accessing iPhone compass with JavaScript

跟風遠走 提交于 2019-12-19 06:21:13

问题


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.


回答1:


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




回答2:


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.




回答3:


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/



来源:https://stackoverflow.com/questions/2564219/accessing-iphone-compass-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!