2016-12-02 24 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(); 
    }); 
    }, 
相關問題