javascript - React Native 无法调用 BackgroundGeolocation.start
问题描述
我收到此错误:
Could not invoke BackgroundGeolocation.start
我无法添加更多详细信息,因为我没有收到任何其他警告或错误。我不知道为什么会导致这个错误。底部的这段代码来自示例,但被翻译为基于函数而不是类。
这是我使用的库: https ://github.com/mauron85/react-native-background-geolocation
useEffect(() => {
if (!true) {
return;
}
BackgroundGeolocation.configure({
desiredAccuracy: BackgroundGeolocation.HIGH_ACCURACY,
stationaryRadius: 50,
distanceFilter: 50,
notificationTitle: 'Background tracking',
notificationText: 'enabled',
//debug: true,
startOnBoot: false,
stopOnTerminate: true,
locationProvider: BackgroundGeolocation.DISTANCE_FILTER_PROVIDER,
interval: 10000,
fastestInterval: 5000,
activitiesInterval: 10000,
stopOnStillActivity: false,
url: 'http://192.168.81.15:3000/location',
httpHeaders: {
'X-FOO': 'bar',
},
// customize post properties
postTemplate: {
lat: '@latitude',
lon: '@longitude',
foo: 'bar', // you can also add your own properties
},
});
BackgroundGeolocation.on('location', (location) => {
console.log('loc', location);
setLocation((prev) => ({
...prev,
latitude: location.latitude,
longitude: location.longitude,
}));
setHistory((prev) => {
setDistance((prevDistance) => {
if (prev.length === 0) {
return 0;
}
const latestItem = prev[prev.length - 1];
return (
prevDistance
);
});
return prev.concat({
latitude: location.latitude,
longitude: location.longitude,
});
});
// handle your locations here
// to perform long running operation on iOS
// you need to create background task
BackgroundGeolocation.startTask((taskKey) => {
// execute long running task
// eg. ajax post location
// IMPORTANT: task has to be ended by endTask
BackgroundGeolocation.endTask(taskKey);
});
});
BackgroundGeolocation.on('stationary', (stationaryLocation) => {
// handle stationary locations here
});
BackgroundGeolocation.on('error', (error) => {
//console.log('[ERROR] BackgroundGeolocation error:', error);
});
BackgroundGeolocation.on('start', () => {
//console.log('[INFO] BackgroundGeolocation service has been started');
});
BackgroundGeolocation.on('stop', () => {
//console.log('[INFO] BackgroundGeolocation service has been stopped');
});
BackgroundGeolocation.on('authorization', (status) => {
console.log(
'[INFO] BackgroundGeolocation authorization status: ' + status,
);
if (status !== BackgroundGeolocation.AUTHORIZED) {
// we need to set delay or otherwise alert may not be shown
setTimeout(
() =>
Alert.alert(
'App requires location tracking permission',
'Would you like to open app settings?',
[
{
text: 'Yes',
onPress: () => BackgroundGeolocation.showAppSettings(),
},
{
text: 'No',
onPress: () => console.log('No Pressed'),
style: 'cancel',
},
],
),
1000,
);
}
});
BackgroundGeolocation.on('background', () => {
console.log('[INFO] App is in background');
});
BackgroundGeolocation.on('foreground', () => {
console.log('[INFO] App is in foreground');
});
BackgroundGeolocation.checkStatus((status) => {
console.log(
'[INFO] BackgroundGeolocation service is running',
status.isRunning,
);
console.log(
'[INFO] BackgroundGeolocation services enabled',
status.locationServicesEnabled,
);
console.log(
'[INFO] BackgroundGeolocation auth status: ' + status.authorization,
);
BackgroundGeolocation.start()
});
return () => {
console.log('Removing all listeners');
BackgroundGeolocation.removeAllListeners();
};
}, [location]);
const [location, setLocation] = useState({
latitude: 37.78825,
longitude: -122.4324,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
});
const [history, setHistory] = useState([]);
const [distance, setDistance] = useState(0);
解决方案
推荐阅读
- php - Laravel 5.6: ErrorException: compact(): Undefined variable: operator
- xml - 如何从 XmlEvent 访问 XML 元素名称
- flutter - 当该属性更改时,如何对显示对象属性的文本小部件进行反应性更新?这与 GetX
- elasticsearch - 如何在一个仪表板kibana中显示不同的时间段?
- linux - 为什么 ldconfig 能够找到一个库,而不是 Rust?
- c# - 并发 AWS Lambdas 运行不止一次
- npm - 将依赖项安装为 .tgz 的 NPM 包
- python - Numpy 数组 - 使用 reshape 将数组末尾的多列堆叠为行
- python - django:粘贴使用文本区域中的选择文件按钮收集的 pdf 文件的文本
- ios - 如何在 pdf 视图上移动/拖动多个视图?