You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

67 lines
2.0 KiB

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;