Задача: Экономия заряда батареи за счет отключения определения местоположения
Текущая проблема: Прослушиватель подписки не работает
Рисунок для внимания
Цель: Если я запущу watchPosition и попытаюсь отключить определение местоположения по GPS, этот прослушиватель подписки станет Не функция не будет отображаться как ошибка.
Код: Выделить всё
useEffect(() => {
watchPosition()
return () => {
Geolocation.clearWatch(watchId)
}
}, [])
useEffect(() => {
requestLocationPermission()
return () => null
}, [])
const requestLocationPermission = async () => {
if (Platform.OS === 'ios') {
const auth = await Geolocation.requestAuthorization("whenInUse");
if (auth === "granted") {
// updateCurrentLocation()
return true
}
} else {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
'title': 'Project Driver App',
'message': 'Project Driver access to your location '
}
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
// updateCurrentLocation()
return true
}
}
}
const updateCurrentLocation = async () => {
Geolocation.getCurrentPosition(
(location) => {
mapRef?.current?.animateToRegion({
longitude: location?.coords?.longitude,
latitude: location?.coords?.latitude,
latitudeDelta: latitudeDelta,
longitudeDelta: longitudeDelta,
}, 1000);
dispatch(DriverCurrentCoordinates({
current_lat:location?.coords?.latitude,
current_long:location?.coords?.longitude
}))
setCurrentCoordinate([location?.coords?.latitude, location?.coords?.longitude])
},
(error) => {
if(error.message == 'No location provider available.') {
// navigation.navigate('Home')
// Geolocation.stopObserving()
}
console.log(error.code, error.message);
},
{ enableHighAccuracy: true, timeout: 15000, maximumAge: 0 }
);
}
const watchPosition = async () => {
watchId.current = Geolocation.watchPosition((position) => {
if(position?.coords?.accuracy < 50) {
setCurrentCoordinate([position?.coords?.latitude, position?.coords?.longitude])
mapRef?.current?.animateToRegion({
latitude: position?.coords?.latitude,
longitude: position?.coords?.longitude,
latitudeDelta: latitudeDelta,
longitudeDelta: longitudeDelta,
},1000)
}
}, {
}, { enableHighAccuracy: true,maximumAge: 0})
}
Подробнее здесь: https://stackoverflow.com/questions/783 ... the-locati