Skip to content

Commit

Permalink
fix location fetching issues (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickdemers6 authored Dec 1, 2022
1 parent f294b45 commit f21b9a9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
12 changes: 9 additions & 3 deletions App.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ import Main from './src/Main';

const App = () => {
React.useEffect(() => {
(async () => {
await Location.requestForegroundPermissionsAsync();
})();
const getLocationPermission = async () => {
try {
await Location.requestForegroundPermissionsAsync();
} catch (e) {
console.log('requestForegroundPermissionsAsync failed');
}
};

setTimeout(getLocationPermission, 1000);
}, []);

return (
Expand Down
17 changes: 11 additions & 6 deletions src/components/Map/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ const Map = () => {

const getCurrentUserLocation = () => {
(async () => {
const location = await Location.getCurrentPositionAsync();
dispatcher?.setUserLocation(location);
})().catch(() => {
console.log('No location permissions granted.');
});
try {
const location = await Location.getCurrentPositionAsync();
dispatcher?.setUserLocation(location);
} catch (e) {
console.log('No location permissions granted.');
}
})();
};

const moveMapToUser = () => {
Expand All @@ -70,9 +72,12 @@ const Map = () => {
};

React.useEffect(updateVehiclesOnForeground, [route]);
React.useEffect(getCurrentUserLocation, []);
React.useEffect(moveMapToUser, [location]);

// get current user location on load and if dispatcher changes
React.useEffect(getCurrentUserLocation, []);
React.useEffect(getCurrentUserLocation, [dispatcher]);

return (
<View style={{ width, height }}>
<MapView
Expand Down

0 comments on commit f21b9a9

Please sign in to comment.