我们来看下react-native的geolocation
来看一下这个包
看下文档吧
看下示例代码
...
import Geolocation from 'react-native-geolocation-service';
...
componentDidMount() {
if (hasLocationPermission) {
Geolocation.getCurrentPosition(
(position) => {
console.log(position);
},
(error) => {
// See error code charts below.
console.log(error.code, error.message);
},
{ enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }
);
}
}
我们看下android的强信号和弱信号分别怎么处理
强信号
const highGeolocation = () => {
Geolocation.getCurrentPosition(
async (position) => {
await updateCoords(position);
},
(error) => {
console.log('high geolocation current position: ', error.code, error.message);
lowGeolocation();
},
{enableHighAccuracy: true, timeout: 2500, maximumAge: 10000}
);
}
弱信号
const lowGeolocation = () => {
Geolocation.getCurrentPosition(
async (position) => {
await updateCoords(position);
},
(error) => {
console.log('low geolocation current position: ', error.code, error.message);
setLocationEnable(false);
},
{enableHighAccuracy: false, timeout: 2500, maximumAge: 10000, forceLocationManager: true}
);
}
然后我们来看下watchPosition
watchPositionId = Geolocation.watchPosition(
async (position) => {
await updateCoords(position);
},
error => {
console.log(error);
},
{
accuracy: {
android: 'high',
ios: 'best',
},
distanceFilter: -1,
interval: 2000
}
);
这里的distanceFilter可以设置成0
也可以设置成-1
根据不同的场景和需求
要合理判断
再看下文档里的说明
Location timeout still happening ?
Try the following steps: (Taken from here)
Turn phone off/on
Turn GPS off/on
Disable any battery saver settings, including Power Saving Mode, Battery Management or any third party apps
Perform an "AGPS reset": Install the App GPS Status & Toolbox, then in that app, go to Menu > Tools > Manage A-GPS State > Reset
Adjusting battery saver settings on different devices:
HTC: Access your phone settings > battery > power saving mode > battery optimization > select your app > don't optimize > save
Huawei: Turn Energy Settings to Normal and add your app to "Protected Apps"
LG If you're running Android 6 or higher: Settings > battery & power saving > battery usage > ignore optimizations > turn ON for your app
Motorola If you're running Android 6 or higher: Battery > select the menu in the upper right-hand corner > battery optimization > not optimized > all apps > select your app > don't optimize
OnePlus (using OxygenOS Settings): Battery > battery optimization > switch to 'all apps' > select your app > don't optimize
Samsung: Access battery settings > app power saving > details > your app > disabled
Sony If you're running Android 6 or higher: Battery > from the menu in the upper right-hand corner > battery optimization > apps > your app
Xiaomi (MIUI OS) If you're running Android 6 or higher: Access your phone settings > additional settings > battery and performance > manage battery usage > apps > your app