如果最初关闭位置服务,则在打开时不会调用watchPosition
。作为一种解决方法,如果发生错误,我将以递归方式调用navigator.geolocation.getCurrentPosition
。
关闭位置服务时,也不会调用watchPosition
。
在iOS上,一切都按预期工作。
任何建议都将不胜感激
在Galaxy Note 5(Android 6.0.1)和不同的AVD仿真器上进行测试,react-native v0.33
android.permission.ACCESS_FINE_LOCATION
包含在AndroidManifest.xml
initialGeolocation(){
delete this.androidLocation;
navigator.geolocation.getCurrentPosition(
(position) => {
log('LOCATION getCurrentPosition success', position);
deviceActions.geolocation(position);
},
(error) => {
log('LOCATION getCurrentPosition error', error.message || error);
if (Platform.OS == 'android'){
this.androidLocation = setTimeout(this.initialGeolocation, 500);
}
},
{
enableHighAccuracy: (Platform.OS == 'android') ? false : true,
timeout: 10000,
maximumAge: 500
}
);
},
componentDidMount() {
this.initialGeolocation();
this.watchID = navigator.geolocation.watchPosition(
(position) => {
log('LOCATION watchPosition success', position);
deviceActions.geolocation(position);
},
(error) => {
log('LOCATION watchPosition error', error.message || error);
},
{
enableHighAccuracy: (Platform.OS == 'android') ? false : true,
distanceFilter: 10,
timeout: 10000,
maximumAge: 500
}
);
}