-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.js
113 lines (101 loc) · 2.63 KB
/
App.js
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import React, { useRef } from 'react';
import {
ActivityIndicator,
PermissionsAndroid,
Platform,
StyleSheet,
View,
} from 'react-native';
import Geolocation from "react-native-geolocation-service"
import { useEffect, useState } from 'react';
import MapView, { Marker} from 'react-native-maps';
const App = () => {
const [loading, setLoading] = useState(true);
const [region, setRegion] = useState({
latitude: 28.3949,
longitude: 84.1240,
latitudeDelta: 0.005,
longitudeDelta: 0.005,
});
const mapRef = useRef(null);
const watchId = useRef(null)
useEffect(()=>{
requestPermission();
return ()=> {
if(watchId.current)
Geolocation.clearWatch(watchId.current)
}
},[])
useEffect(()=>{
if(loading) return;
mapRef.current?.animateToRegion(region);
},[region])
const requestPermission = async () => {
if (Platform.OS == "ios") {
const auth = await Geolocation.requestAuthorization('whenInUse');
getCurrentLocation();
} else {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
);
console.log(granted);
if (granted === PermissionsAndroid.RESULTS.GRANTED) getCurrentLocation();
else {
alert('notGranted')
}
}
};
const getCurrentLocation = () => {
watchId.current = Geolocation.watchPosition(
(position) => {
setRegion({
...region,
latitude: position.coords.latitude,
longitude: position.coords.longitude,
});
},
(error) => {
alert('Unable to locate your location')
},
{ enableHighAccuracy: true, distanceFilter:0.01},
);
setLoading(false);
};
if(loading) return <View style={styles.container}>
<ActivityIndicator size={55} color="grey"/>
</View>
return (
<View style={{flex: 1}}>
<MapView
ref={mapRef}
style={styles.map}
initialRegion={region}
>
{[{
latLong: {latitude:region.latitude,longitude:region.longitude},
title: 'You Current Location',
description: 'This is your location'
}].map((marker, index) => (
<Marker.Animated
key={index}
coordinate={marker.latLong}
title={marker.title}
description={marker.description}
/>
))}
</MapView>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'white',
},
map: {
...StyleSheet.absoluteFillObject,
},
});
export default App;