2016-12-02 59 views
10

我在我的应用上使用airbnb's map作为react-native。这个问题是为Android应用程序,因为我还没有解决iOS应用程序。在React Native AirBnb的MapView上关注用户位置[Android]

我的问题:

navigator.geolocation正常工作在模拟器,但时间过长在真实设备上,给点有时超时旧设备。

我已经注意到:

如果showsUserLocation道具设置为true,我可以看到在地图上的方式本土“当前位置”标志之前getCurrentPosition缓解。

我想要什么:

  1. 我要永远对用户的位置为中心我所在的地区。
  2. 我想要一种方法来访问本地地理位置api(应该更快?),或者有人告诉我我做错了什么。以下是 的一个代码示例。
  3. 即使位置服务已打开/关闭,我仍然能够检测到用户的位置。这是showsUserLocation道具的行为,但它似乎一旦watchPostion错误,它完全停止。

这里是我到目前为止,这是非常简单的,因为它是:

如果
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} 
    ); 
    } 
+0

您应该离开办公室或家中使用设备进行位置测试。在真实设备中必须为'enableHighAccuracy:true'。它对我来说是完美的。 –

+0

检查它这个问题http://stackoverflow.com/a/38125402/2955679 –

+0

@pedropeixoto我做了所有安装指南中要求的,但showsUserLocation不适用于我。你能帮我吗。? – HelpingHand

回答

4

我一直觉得watchPosition iffy在不同的浏览器之间,我以前的一件事是用getCurrentPosition代替setInterval,以便它每隔几秒就会得到一个位置,如果它一旦失败, y下一个时间间隔,不像watchPosition,如果发生错误,它将停止监视。

一般浏览器的地理位置并不十分可靠,您可以检查此线程的一些问题https://github.com/facebook/react-native/issues/7495

另一种选择,这肯定会更快,也许更可靠的方法是使用本地地理位置API。检查此问题是否存在暴露原生地理位置的库,以便对原生地理位置作出反应React-native Android geolocation

+1

在你发送的链接上有很多东西需要消化,但它会让我继续前进。赏金即将结束,所以我会奖励你。谢谢! – pedropeixoto

+1

谢谢。底线是使用本地地理位置插件以获得更高的可靠性。祝你好运 – Gaafar

0

不知道这将解决你的问题,但试试这个:

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(); 
    }); 
    },