React Native Android端出现Location Error 'Google play services is not available'求助
解决react-native-geolocation-service Android端“Google play services is not available”错误
我之前在项目里也碰到过一模一样的问题,iOS端顺风顺水,Android这边死活报GMS不可用的错,折腾了好一阵才搞定,给你分享几个排查和解决的关键点:
1. 先确认Google Play Services依赖配置是否到位
react-native-geolocation-service在Android上是依赖Google Play Services定位组件的,所以首先得检查你的gradle配置:
- 打开
android/app/build.gradle,确保添加了GMS定位依赖(版本可以用最新稳定版):
dependencies { // 其他现有依赖... implementation 'com.google.android.gms:play-services-location:21.0.1' }
- 再检查项目根目录的
build.gradle,确认已经配置了Google Maven仓库,不然会拉取不到GMS依赖:
allprojects { repositories { // 其他仓库比如jcenter()、mavenCentral()... google() // 必须要有这个 } }
配置完记得同步gradle,清理一下项目缓存再重新编译。
2. 检查设备/模拟器的Google Play Services状态
这个是最常见的原因:
- 实体设备:如果是国内定制ROM的手机(比如小米、华为的部分机型),可能没有预装Google Play Services,或者版本太旧。可以去设置→应用管理里找Google Play Services,查看版本,要是没有的话,得先安装GMS框架;要是版本老,就去Google Play商店更新到最新版。
- 模拟器:创建模拟器的时候一定要选带「Google APIs」或者「Google Play」的系统镜像,纯Android镜像没有GMS。如果已经创建了模拟器,打开模拟器里的Google Play应用,检查是否有更新提示,更新完重启模拟器再试。
3. 权限配置要完整且动态申请
虽然你说已经开了权限,但再仔细核对下:
- 首先在
AndroidManifest.xml里添加必要的权限:
<!-- 精确定位权限 --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- 粗略定位权限 --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- Android 10+后台定位需要的权限(如果你的App需要后台定位) --> <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" /> <!-- 检查GMS需要联网权限 --> <uses-permission android:name="android.permission.INTERNET" />
- 然后Android 6.0+必须动态申请权限,不能只靠Manifest配置。在调用
watchPosition之前,先请求定位权限:
import { PermissionsAndroid } from 'react-native'; import Geolocation from 'react-native-geolocation-service'; async function requestLocationPermissions() { try { const granted = await PermissionsAndroid.requestMultiple([ PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, PermissionsAndroid.PERMISSIONS.ACCESS_BACKGROUND_LOCATION, // 如果不需要后台定位可以去掉 ]); return granted['android.permission.ACCESS_FINE_LOCATION'] === PermissionsAndroid.RESULTS.GRANTED; } catch (err) { console.warn('申请权限出错:', err); return false; } } // 在组件的生命周期里调用,比如componentDidMount: async componentDidMount() { const hasPermission = await requestLocationPermissions(); if (hasPermission) { // 权限通过后再启动定位监听 this.watchId = Geolocation.watchPosition( position => { console.log('获取到位置:', position); this.dispatch({ type: UPDATE_LOCATION, location: position }); }, error => { console.log('定位错误:', error); }, { enableHighAccuracy: true, distanceFilter: 0, interval: 10000, fastestInterval: 5000 } ); } else { console.log('定位权限被拒绝'); } }
4. 先检查GMS可用性再启动定位
可以在代码里先判断Google Play Services是否可用,避免直接调用定位报错:
Geolocation.checkGooglePlayServices() .then(isAvailable => { if (isAvailable) { // GMS可用,再启动定位监听 this.watchId = Geolocation.watchPosition(...); } else { console.log('Google Play Services不可用,请检查设备配置'); // 这里可以给用户弹出提示,引导更新或安装GMS } }) .catch(err => { console.log('检查GMS状态出错:', err); });
5. 无GMS设备的替代方案
如果你的目标用户有很多用没有GMS的设备(比如国内大部分安卓手机),那react-native-geolocation-service可能不太适用,可以考虑改用react-native-location库,它支持Android原生的定位API,不需要依赖Google Play Services,配置和使用也类似。
内容的提问来源于stack exchange,提问作者noone
相关产品推荐
相关产品推荐

