我正在使用react-native创建一个应用程序,需要根据用户移动的距离更新用户位置。我正在使用navigator.geolocation.watchPosition
作为事件监听器,但它没有按预期运行。这是我的测试代码:
export default class geoApp extends Component {
constructor() {
super();
this.state = {
lastPos: 'unknown',
}
this.watchID = null;
}
componentDidMount() {
this.watchID = navigator.geolocation.watchPosition(
(pos) => {
const lastPos = JSON.stringify(pos.coords)
this.setState({ lastPos: lastPos });
},
(error) => {
alert(JSON.stringify(error))
},
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 0, distanceFilter: 1 }
);
}
componentWillUnmount() {
navigator.geolocation.clearWatch(this.watchID);
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
{this.state.lastPos}
</Text>
</View>
);
}
}
我尝试删除选项对象或只更改数据但没有任何效果。
如果我没有放置选项对象,那么回调将会触发一次,并且通常会在20秒之后触发,如果我保留选项对象,它甚至不会触发。
我认为这是因为maximumAge
,但我试图将不同的数据放入其中,但没有任何效果。
我甚至尝试navigator.geolocation.getCurrentLocation
并将其包裹在setInterval
内,但它始终返回相同的信息。
任何帮助都会被激活,谢谢!
注意:我的清单中有<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
反应和反应 - 原生版本如下:
"react": "16.0.0-alpha.12",
"react-native": "0.47.2"
我正在尝试在Android华为P9 lite上运行。