Anonymous
Как исправить: Прослушиватель подписки не работает при отключении определения местоположения GPS?
Сообщение
Anonymous » 17 апр 2024, 21:36
как исправить, что прослушиватель подписки не работает, когда я отключаю определение местоположения по GPS. Честно говоря, я не знаю, что это за ошибка и почему эта ошибка отображается, когда я отключаю определение местоположения по GPS.
Задача: Экономия заряда батареи за счет отключения определения местоположения
Текущая проблема: Прослушиватель подписки не работает
Рисунок для внимания
Цель: Если я запущу 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
1713379014
Anonymous
как исправить, что прослушиватель подписки не работает, когда я отключаю определение местоположения по GPS. Честно говоря, я не знаю, что это за ошибка и почему эта ошибка отображается, когда я отключаю определение местоположения по GPS. [b]Задача:[/b] Экономия заряда батареи за счет отключения определения местоположения [b]Текущая проблема:[/b] Прослушиватель подписки не работает Рисунок для внимания [b]Цель:[/b] Если я запущу watchPosition и попытаюсь отключить определение местоположения по GPS, этот прослушиватель подписки станет Не функция не будет отображаться как ошибка. [code]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}) } [/code] Спасибо. Подробнее здесь: [url]https://stackoverflow.com/questions/78343147/how-to-fix-subscription-listener-is-not-a-function-when-turning-off-the-locati[/url]