2017-01-01 163 views
6

我在我的反應本地項目中使用navigator.geolocation.watchPosition在用戶移動時在地圖上繪製路徑。我注意到這個功能的返回頻率非常低。我教過它至少是頻率,當時我使用iOS仿真器和GPS仿真器中的「高速公路驅動器」模式進行測試。現在,當我用「城市跑步」進行測試時,我可以看到,位置的返回頻率不取決於某個時間間隔,而是取決於距離......該功能每100米返回一次位置,無論這個職位需要多長時間才能改變。navigator.geolocation.watchPosition只返回每100米

這是爲什麼?這是一個預期的行爲?我不知道它是否與iOS模擬器或我的代碼有關,但我真的希望更準確的位置,我希望它儘可能經常返回。

componentDidMount() { 
    const { region } = this.state; 

    navigator.geolocation.getCurrentPosition(
     (position) => { 
      this.setState({position}); 
     }, 
     (error) => alert(JSON.stringify(error)), 
     {enableHighAccuracy: true, timeout: 20000, maximumAge: 1000} 
    ); 

    this.watchID = navigator.geolocation.watchPosition((lastPosition) => { 
     var { distanceTotal, record } = this.state; 
     this.setState({lastPosition}); 
     if(record) { 
      var newLatLng = {latitude:lastPosition.coords.latitude, longitude: lastPosition.coords.longitude}; 

      this.setState({ track: this.state.track.concat([newLatLng]) }); 
      this.setState({ distanceTotal: (distanceTotal + this.calcDistance(newLatLng)) }); 
      this.setState({ prevLatLng: newLatLng }); 
     } 
    }, 
    (error) => alert(JSON.stringify(error)), 
    {enableHighAccuracy: true, timeout: 20000, maximumAge: 0}); 
} 
+1

我有完全相同的問題。你有沒有找到解決這個問題的方法?謝謝。 – Larney

回答

15

您可以設置一個選項,名爲distanceFilter,您可以以米爲單位設置精度。它在documentation for geolocation中陳述,但沒有解釋它做了什麼或默認值。如果您查看github的源代碼,則默認設置爲米,這解釋了您的行爲。

如果你想精度1米,你設置的選項: {enableHighAccuracy: true, timeout: 20000, maximumAge: 0, distanceFilter: 1}