the*_*eva 14 geolocation react-native react-native-maps
我在我的反应本机项目中使用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});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9703 次 |
| 最近记录: |