-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlocalizacao.tsx
More file actions
55 lines (48 loc) · 2.04 KB
/
Copy pathlocalizacao.tsx
File metadata and controls
55 lines (48 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import React, { useState, useEffect } from 'react';
import { PermissionsAndroid, Platform, Alert } from 'react-native';
import Geolocation from '@react-native-community/geolocation';
const useLocation = () => {
const [currentLatitude, setCurrentLatitude] = useState(null);
const [currentLongitude, setCurrentLongitude] = useState(null);
useEffect(() => {
const getLocation = () => {
Geolocation.getCurrentPosition(
(position) => {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
setCurrentLatitude(latitude);
setCurrentLongitude(longitude);
},
(error) => {
console.error('Erro ao obter a localização:', error);
Alert.alert('Erro', 'Não foi possível obter a localização.');
},
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
);
};
const requestLocationPermission = async () => {
if (Platform.OS === 'android') {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: 'Permissão de Acesso à Localização',
message: 'Este aplicativo precisa acessar sua localização.',
buttonNeutral: 'Pergunte-me depois',
buttonNegative: 'Cancelar',
buttonPositive: 'OK'
}
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
getLocation();
} else {
Alert.alert('Permissão de Acesso negada');
}
} else {
getLocation();
}
};
requestLocationPermission();
}, []);
return { currentLatitude, currentLongitude };
};
export default useLocation;