我目前正在 React Native 应用程序中使用 react-native-background-geolocation 库进行地理围栏并更新用户在 Google 地图上的当前位置。一切都很顺利,直到最近,当我开始遇到以下错误时:
错误不变违规:没有找到 RNBackgroundGeolocation.watchPosition 的 cbID 747 和 callID 373 的回调 - 很可能回调已被调用。参数:'[1]',js引擎:hermes
This error typically occurs when the user has disabled location services
on their device. To prevent this error from occurring, you can check
whether location services are enabled before calling the react-native-
background-geolocation functions to get the current location.
One way to achieve this is by using the react-native-android-location-
enabler library. You can install it using npm:
npm install react-native-android-location-enabler
import { promptForEnableLocationIfNeeded } from 'react-native-android-
location-enabler';
import { Platform } from 'react-native';
async function handleCheckPressed() {
if (Platform.OS === 'android') {
try {
const enableResult = await promptForEnableLocationIfNeeded();
console.log('enableResult', enableResult);
// Handle enableResult based on your application's logic
} catch (error: unknown) {
if (error instanceof Error) {
console.error(error.message);
// Handle the error appropriately
}
}
}
}
By using this approach, you can ensure that the necessary location
services are enabled before making calls to react-native-background-
geolocation, thereby preventing the occurrence of the error.