2017-01-01 158 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}