-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
88 lines (80 loc) · 2.25 KB
/
App.tsx
File metadata and controls
88 lines (80 loc) · 2.25 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import Geolocation from '@react-native-community/geolocation';
import { StatusBar } from 'expo-status-bar';
import { useEffect, useState } from 'react';
import GeoLocationLocal from 'react-native-geolocation-service';
import { PermissionsAndroid, Platform, StyleSheet, Text, View } from 'react-native';
import MapView from 'react-native-maps';
export default function App() {
const [latitude, setLat] = useState(0);
const [longitude, setLon] = useState(0);
async function permicoesParaLocalizacao(){
try {
if(Platform.OS === 'ios'){
await Geolocation.requestAuthorization();
}else {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,{
title: "Para continuar é necessário autorizar o uso da localização",
message: "Sua localização será usada para mostrarmos no mapa",
buttonNegative: "Negar",
buttonPositive: "Permitir"
});
}
pegarLocalizacaoAtual();
} catch (err) {
console.warn('erro Permissions: ' ,err);
}
}
useEffect(()=>{
permicoesParaLocalizacao()
},[]);
async function pegarLocalizacaoAtual(){
GeoLocationLocal.getCurrentPosition(
(position) => {
setLat(position.coords.latitude);
setLon(position.coords.longitude);
},
(error) => {
console.log('erro ao pegar posições: ',error);
return null;
},
{
enableHighAccuracy: true, // precisão
timeout: 15000,
maximumAge: 10000 // tempo para manter armazenado local anterior em cache
}
)
}
if(latitude==0){
return (
<View style={styles.container}>
<Text>Carregando GPS</Text>
<StatusBar style="auto" />
</View>
);
}else{
return(
<>
<StatusBar style="auto" />
<MapView
style={{flex: 1}}
showsUserLocation={true}
region={{
latitude: latitude,
longitude: longitude,
latitudeDelta: 0.2122,
longitudeDelta: 0.2121,
}}>
</MapView>
</>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});