import { StatusBar } from "expo-status-bar";
import React, { useState, useEffect } from "react";
import { StyleSheet, Text, View } from "react-native";
import * as Location from "expo-location";
export default function App() {
const [location, setLocation] = useState(null);
const [errorMsg, setErrorMsg] = useState(null);
const [locationEnabled, setLocationEnabled] = useState(false);
useEffect(() => {
Location.watchPositionAsync(
{
accuracy: Location.Accuracy.Highest,
distanceInterval: 1,
timeInterval: 10000,
},
(pos) => {
setLocation(pos.coords);
}
)
.then((locationWatcher) => {
//set locationwatcher for removing
})
.catch((err) => {
/*(async () => {
let res = await Location.hasServicesEnabledAsync();
console.log("watchpos location2: ", res);
setLocationEnabled(res);
})();*/
console.log("watchpositon error: ", err.message);
setErrorMsg(err.message);
});
}, []);
useEffect(() => {
console.log(locationEnabled);
}, [locationEnabled, location]);
let text = "Waiting..";
if (errorMsg) {
text = errorMsg;
} else if (location) {
text = JSON.stringify(location);
}
return (
<View style={styles.container}>
<Text style={styles.paragraph}>{JSON.stringify(location)}</Text>
</View>
);
}
In my example, the docs say, that watchPositionAync() subscribes to user location changes, so I thought the error when the location sharing is disabled it will be in the catch(), but it is not. If I disable location it just won’t get called, but if I enable it it will continue.
How can I check if the location was disabled? I tried using hasServicesEnabledAsync(), but I don’t know where to call it.