Skip to content

Commit f21b9a9

Browse files
fix location fetching issues (#66)
1 parent f294b45 commit f21b9a9

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

App.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@ import Main from './src/Main';
88

99
const App = () => {
1010
React.useEffect(() => {
11-
(async () => {
12-
await Location.requestForegroundPermissionsAsync();
13-
})();
11+
const getLocationPermission = async () => {
12+
try {
13+
await Location.requestForegroundPermissionsAsync();
14+
} catch (e) {
15+
console.log('requestForegroundPermissionsAsync failed');
16+
}
17+
};
18+
19+
setTimeout(getLocationPermission, 1000);
1420
}, []);
1521

1622
return (

src/components/Map/Map.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@ const Map = () => {
4747

4848
const getCurrentUserLocation = () => {
4949
(async () => {
50-
const location = await Location.getCurrentPositionAsync();
51-
dispatcher?.setUserLocation(location);
52-
})().catch(() => {
53-
console.log('No location permissions granted.');
54-
});
50+
try {
51+
const location = await Location.getCurrentPositionAsync();
52+
dispatcher?.setUserLocation(location);
53+
} catch (e) {
54+
console.log('No location permissions granted.');
55+
}
56+
})();
5557
};
5658

5759
const moveMapToUser = () => {
@@ -70,9 +72,12 @@ const Map = () => {
7072
};
7173

7274
React.useEffect(updateVehiclesOnForeground, [route]);
73-
React.useEffect(getCurrentUserLocation, []);
7475
React.useEffect(moveMapToUser, [location]);
7576

77+
// get current user location on load and if dispatcher changes
78+
React.useEffect(getCurrentUserLocation, []);
79+
React.useEffect(getCurrentUserLocation, [dispatcher]);
80+
7681
return (
7782
<View style={{ width, height }}>
7883
<MapView

0 commit comments

Comments
 (0)