在 iphon 和 ipad 上的网络浏览器中访问指南针

问题描述 投票:0回答:0

我正在使用运行 16.3.1 的 ipad pro 和运行 16.1.1 的 iphone Xs Max。 我正在尝试创建一个需要访问指南针的网页。 由于这是一个私人项目,我不担心 Android 设备。

这里是我使用的测试代码:

<!DOCTYPE html>
<html>
<head>
 <title>Compass Heading</title>
</head>
<body>
  <h1>Compass Heading: <span id="heading">not changed</span>°</h1>

 <script>
           // check if the device has a compass
           if ('ondeviceorientation' in window && 'webkitCompassHeading' in event) {
            // listen for changes in the device's orientation
            window.addEventListener('deviceorientation', function(event) {
                //alert('event fired');
                // get the compass heading in degrees
                var heading = event.webkitCompassHeading;
                // update the heading on the page
                document.getElementById('heading').textContent = heading.toFixed(2);
            });
        } else {
            alert('Sorry, your device does not have a compass.');
        }
    </script>

</body>
</html>

我没有收到警报,但我也没有收到任何文本更改(未更改)。我尝试将警报放入事件侦听器中,但未显示。

我已确保在两台设备上的定位服务中将指南针设置为“应用内”,并且指南针应用程序可在两台设备上运行。

我读过其他帖子的相互矛盾的评论,大多数都很旧。 有人可以给我最新的最简单的解决方案吗?

javascript ios iphone ipad
© www.soinside.com 2019 - 2024. All rights reserved.