forked from cemaden-educacao/WPD-MobileApp
analuizaff
4 years ago
11 changed files with 127 additions and 57 deletions
-
3src/App.js
-
3src/app/components/EventLocationInput.js
-
4src/app/config/globals.js
-
67src/app/context/CurrentLocationContext.js
-
29src/app/context/EventLocationContext.js
-
4src/app/hooks/useLocation.js
-
25src/app/screens/MapFeedScreen.js
-
25src/app/screens/MapFormScreen.js
-
2src/app/screens/RiverFloodSharingDataScreen.js
-
6src/app/screens/SharingDataScreen.js
-
2src/app/screens/SharingFloodZonesScreen.js
@ -0,0 +1,67 @@ |
|||||
|
import React, { useState, createContext, useEffect } from "react" |
||||
|
import * as Location from "expo-location"; |
||||
|
|
||||
|
|
||||
|
export const CurrentLocationContext = createContext(); |
||||
|
|
||||
|
const CurrentLocationProvider = ({ children }) => { |
||||
|
const [currentLocation, setCurrentLocation] = useState("endereço usuário"); |
||||
|
const [currentCoordinates, setCurrentCoordinates] = useState({ longitude: 0.0, latitude: 0.0 }); |
||||
|
|
||||
|
//get user current location coordinates
|
||||
|
const getCoordinates = async () => { |
||||
|
try { |
||||
|
const { granted } = await Location.requestPermissionsAsync(); |
||||
|
|
||||
|
if (!granted) return; |
||||
|
|
||||
|
const { |
||||
|
coords: { latitude, longitude }, |
||||
|
} = await Location.getCurrentPositionAsync({ |
||||
|
accuracy: Location.Accuracy.Highest, |
||||
|
}); |
||||
|
|
||||
|
setCurrentCoordinates({ latitude, longitude }); |
||||
|
//console.log({ latitude, longitude });
|
||||
|
// console.log(currentCoordinates);
|
||||
|
|
||||
|
getAddress({ latitude, longitude }); |
||||
|
|
||||
|
} catch (error) { |
||||
|
console.log(error); |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
//get user current location address
|
||||
|
const getAddress = async (coordenadas) => { |
||||
|
// console.log("PEGANDO ENDEREÇO");
|
||||
|
Location.setGoogleApiKey("AIzaSyD_wuuokS3SVczc8qSASrsBq0E5qIpdyMc"); |
||||
|
|
||||
|
const address = await Location.reverseGeocodeAsync(coordenadas); |
||||
|
setCurrentLocation(address[0].street + ", " + address[0].name + "\n" + address[0].district, coordenadas.x); |
||||
|
//console.log(currentLocation);
|
||||
|
}; |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
useEffect(() => { |
||||
|
getCoordinates(); |
||||
|
//console.log(currentCoordinates.latitude);
|
||||
|
}, []); |
||||
|
const getCurrentLocation = (coordenadas) => { |
||||
|
setCurrentCoordinates(coordenadas); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
return ( |
||||
|
<CurrentLocationContext.Provider value={{ |
||||
|
currentLocation, |
||||
|
currentCoordinates, |
||||
|
getCurrentLocation, |
||||
|
}}> |
||||
|
{children} |
||||
|
</CurrentLocationContext.Provider> |
||||
|
) |
||||
|
} |
||||
|
export default CurrentLocationProvider; |
Write
Preview
Loading…
Cancel
Save
Reference in new issue