【问题标题】:navigator.geolocation.watchPosition only return each 100 mnavigator.geolocation.watchPosition 仅返回每 100 m
【发布时间】:2017-05-15 20:46:44
【问题描述】:

我在我的 react native 项目中使用 navigator.geolocation.watchPosition 在用户移动时在地图上绘制路径。我注意到这个函数的返回频率很低。当我使用 iOS 模拟器和 gps 模拟器中的“高速公路驾驶”模式进行测试时,我至少教过它是频率。现在,当我改用“城市跑”进行测试时,我可以看到位置的返回频率不取决于某个时间间隔,而是取决于距离......该函数每 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});
} 

【问题讨论】:

  • 我遇到了完全相同的问题。你找到解决这个问题的方法了吗?谢谢。

标签: react-native geolocation react-native-maps


【解决方案1】:

您可以设置一个名为distanceFilter 的选项,以米为单位设置精度。它在documentation for geolocation 中有说明,但没有解释它的作用或默认值。如果您查看github 的源代码,默认设置为 100 米,这可以解释您的行为。

如果您想要 1 米的精度,请将选项设置为: {enableHighAccuracy: true, timeout: 20000, maximumAge: 0, distanceFilter: 1}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-28
    • 1970-01-01
    • 2023-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多