-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathContext.js
143 lines (130 loc) · 4.54 KB
/
Context.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import React, {useEffect, useState} from 'react';
import PropTypes from 'prop-types';
import {
Geolocation,
init,
setLocatingWithReGeocode,
setNeedAddress,
} from 'react-native-amap-geolocation';
import {PermissionsAndroid, Platform} from 'react-native';
export const SystemContext = React.createContext({});
export default function AppContext({children}) {
const [systemNavigator, setSystemNavigator] = useState(null);
const [currentLocation, setCurrentLocation] = useState(null);
const [currentPosition, setCurrentPosition] = useState(null);
const [currentCityName, setCityName] = useState(null);
const [showHeader, setShowHeader] = useState(true);
const AMapKey = {
ios: 'YOUR_IOS_KEY_HERE',
android: 'YOUR_ANDROID_KEY_HERE',
web: 'YOUR_WEB_KEY_HERE',
};
useEffect(() => {
const locationTimer = getLocation();
return () => {
Geolocation.clearWatch(locationTimer);
};
}, []);
useEffect(() => {
if (currentLocation) {
if (currentLocation.addressComponent.city.toString().length) {
setCityName(currentLocation.addressComponent.city);
} else {
setCityName(currentLocation.addressComponent.district);
}
} else {
setCityName(null);
}
}, [currentLocation]);
async function getLocation() {
// 对于 Android 需要自行根据需要申请权限
if (Platform.OS === 'android') {
const result = await PermissionsAndroid.requestMultiple([
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION,
]);
console.log(result);
}
await init(AMapKey);
if (Platform.OS === 'android') {
setNeedAddress(true);
} else {
setLocatingWithReGeocode(true);
}
// return Geolocation.watchPosition((position) => {
// setCurrentPosition(position.coords);
// getReadableAddress(position.coords);
// });
}
// https://lbs.amap.com/api/webservice/guide/api/georegeo
function getReadableAddress(location) {
const url =
'https://restapi.amap.com/v3/geocode/regeo?output=json&key=' +
AMapKey.web +
'&radius=100&extensions=all&location=' +
location.longitude +
',' +
location.latitude;
fetch(url, {
method: 'GET',
})
.then((response) => response.json())
.then((responseJson) => {
console.log(responseJson.regeocode);
setCurrentLocation(responseJson.regeocode);
if (
responseJson.regeocode.addressComponent.city.toString()
.length
) {
setCityName(responseJson.regeocode.addressComponent.city);
} else {
setCityName(
responseJson.regeocode.addressComponent.district,
);
}
// console.log(responseJson.regeocode.addressComponent.building);
})
.catch((error) => {
console.error(url, error);
});
}
function getShortAddress() {
if (currentLocation && currentLocation.pois.length) {
// console.log(currentLocation);
if (currentLocation.pois.length > 1) {
currentLocation.pois.sort((a, b) => {
if (parseFloat(a.distance) > parseFloat(b.distance)) {
return 1;
}
if (parseFloat(a.distance) < parseFloat(b.distance)) {
return -1;
}
return 0;
});
}
return currentLocation.pois[0].name;
}
return null;
}
return (
<SystemContext.Provider
value={{
showHeader,
setShowHeader,
systemNavigator,
setSystemNavigator,
currentPosition,
setCurrentPosition,
currentLocation,
setCurrentLocation,
currentCityName,
getShortAddress,
getReadableAddress,
}}>
{children}
</SystemContext.Provider>
);
}
SystemContext.propTypes = {
children: PropTypes.oneOfType([PropTypes.func, PropTypes.array]),
};