iPhone 7 上的 React Native 地理定位标题始终为 -1

React Native geolocation heading is always -1 on iPhone 7

我遇到了一个问题,一个星期都解决不了。

任务:我注意观察设备位置及其方向。

解决方案:我使用文档中描述的方法

this.watcher = navigator.geolocation.watchPosition((position) => {
    const latitude = position.coords.latitude;
    const longitude = position.coords.longitude;
    const heading = position.coords.heading;

    this.setState({
      latitude: latitude,
      longitude: longitude,
      heading: heading
    });
  },
  (error) => {
     console.log(`error: ${error}`);
  }, 
  {
    enableHighAccuracy: true,
    timeout: 10000,
    maximumAge: 0
  });

Eevrything 在模拟器上运行良好。但在真正的 iOS 设备上,航向始终为 -1。 我的项目使用 RN 0.54。但是我刚刚通过 react-native init 使用 RN 0.56 创建了新项目并且行为是相同的。

问题是 react-native-maps 包用户方向以完美的方式呈现,所以我认为 React Native 可能存在一些问题

有什么帮助吗?

使用 Location.getHeadingAsync() 我能够得到一个不为 -1 的航向。

查看此 link 了解我正在使用的 expo 方法: https://docs.expo.io/versions/latest/sdk/location#expolocationgetheadingasync-215

由于 React Native 地理定位是对网络地理定位的扩展 (see docs), 它获得方向(航向)作为最后两个位置之间的差异。这在低速或用户停留时不工作(returns -1)。

这很奇怪,因为我希望 RN 从内置 "compass" 中获取设备航向。

弹出应用程序的解决方案:使用第三方库react-native-heading即使用户停留也能使设备发热。

世博会的解决方案:应该没有这样的问题,因为世博会使用内置指南针而不是网络地理定位。