将 React Native 项目升级到版本 0.74.3 后遇到问题。该应用程序在版本 0.64.4 上运行良好,但升级后,尝试使用 RNFusedLocation.getCurrentPosition 时出现错误
**项目配置详细信息: **
React Native Version: ^0.74.3 (was working fine on ^0.64.4)
Google Play Services Location Version: 21.3.0
Library used: "react-native-geolocation-service": "^5.3.1"
**等级配置: **
googlePlayServicesLocationVersion = "21.3.0" . implementation 'com.google.android.gms:play-services-location:21.3.0'
**错误信息: **
`Could not invoke RNFusedLocation.getCurrentPosition
null
Found interface com.google.android.gms.location.SettingsClient, but class was expected
(declaration of 'com.google.android.gms.location.SettingsClient' appears in /data/app/...)
`
我尝试更新项目中的 Google Play 服务版本。最初,我有:
googlePlayServicesLocationVersion = "21.3.0" // Or higher.
implementation 'com.google.android.gms:play-services-location:21.3.0'
然后我尝试降级为:
googlePlayServicesVersion = "17.0.0"
implementation 'com.google.android.gms:play-services-location:21.3.0'
但是,尽管进行了这些更改,问题仍然存在。
出于某种原因,我猜地理定位已经从这个反应原生版本中提取出来了。我使用的是 .75 版本,我的代码也遇到了同样的错误,
Geolocation.getCurrentPosition(
(position) => {
console.log('Current position:', position);
},
(error) => {
console.error('Geolocation error:', error);
Alert.alert('Error', 'Unable to retrieve location.');
},
{ enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }
);
我安装并使用了@react-native-community/geolocation 包,而不是react-native-geolocation-service 包。
npm install @react-native-community/geolocation --save
然后
import Geolocation from '@react-native-community/geolocation';
这就是我必须更改才能解决问题的全部内容。希望这会有所帮助。