在React Native AirBnb'上跟踪用户位置;s地图视图[Android]

在React Native AirBnb'上跟踪用户位置;s地图视图[Android],android,google-maps,reactjs,react-native,geolocation,Android,Google Maps,Reactjs,React Native,Geolocation,我正在我的应用程序上使用react native。这个问题是针对android应用程序的,因为我还没有熟悉iOS应用程序 我的问题: 在emulator上工作正常,但在真实设备上花费的时间太长,以至于在旧设备上有时会超时 我注意到的: 如果showsUserLocationprop设置为true,我可以在解析之前在地图上看到本机“当前位置”标记 我想要什么: 我希望我的区域始终以用户的位置为中心 我想要一种访问本地地理位置api的方法(应该是 更快?),或者有人告诉我我做错了什么。下面是 代码示

我正在我的应用程序上使用react native。这个问题是针对android应用程序的,因为我还没有熟悉iOS应用程序

我的问题:

在emulator上工作正常,但在真实设备上花费的时间太长,以至于在旧设备上有时会超时

我注意到的:

如果
showsUserLocation
prop设置为true,我可以在解析之前在地图上看到本机“当前位置”标记

我想要什么:

  • 我希望我的区域始终以用户的位置为中心
  • 我想要一种访问本地地理位置api的方法(应该是 更快?),或者有人告诉我我做错了什么。下面是 代码示例
  • 我希望即使在打开/关闭定位服务之后,仍然能够检测到用户的位置。这是
    showsUserLocation
    prop的行为,但似乎一旦watchposition出错,它就会完全停止
  • 到目前为止,我所掌握的情况非常简单:

      componentDidMount() {
        navigator.geolocation.getCurrentPosition(
          (position) => {
            console.log("getCurrentPosition Success");
            this.setState({
              region: {
                ...this.state.region,
                latitude: position.coords.latitude,
                longitude: position.coords.longitude,
              }
            });
            this.props.resetNotifications();
            this.watchPosition();
          },
          (error) => {
            this.props.displayError("Error dectecting your location");
            alert(JSON.stringify(error))
          },
          {enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
        );
      }
    
      watchPosition() {
        this.watchID = navigator.geolocation.watchPosition(
          (position) => {
            console.log("watchPosition Success");
            if(this.props.followUser){
              this.map.animateToRegion(this.newRegion(position.coords.latitude, position.coords.longitude));
            }
          },
          (error) => {
            this.props.displayError("Error dectecting your location");
          },
          {enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
        );
      }
    

    不确定这是否能解决您的问题,但请尝试以下方法:

    navigator.geolocation.getCurrentPosition(
      (position) => {
        console.log("getCurrentPosition Success");
        this.setState({
          region: {
            ...this.state.region,
            latitude: position.coords.latitude,
            longitude: position.coords.longitude,
          }
        }, () => {
          this.props.resetNotifications();
          this.watchPosition();
        });
      },
    

    我总是发现不同浏览器之间的
    watchPosition
    不一致,以前对我有效的一件事是在
    setInterval
    中替换为
    getCurrentPosition
    ,这样它每隔几秒钟就会获得一次位置,如果失败,它将重试下一次间隔,与发生错误时停止监视的
    watchPosition
    不同

    一般来说,浏览器地理位置不是很可靠,您可以检查此线程的一些问题


    另一种肯定会更快、也许更可靠的选择是使用本地地理定位API。对于为react native公开本机地理位置的库,请检查此问题。您应该带着设备离开办公室或家进行位置测试。在实际设备中必须启用高精度:true。这对我来说非常好。检查这个问题@pedropeixoto我完全按照安装指南中的要求做了,但showsUserLocation对我不起作用。你能帮我吗?关于你发送的链接,有很多东西需要消化,但它肯定会让我继续前进。悬赏就要结束了,我会奖励你的。谢谢谢谢底线是使用本地地理定位插件以提高可靠性。祝你好运