React Native安卓APK中GooglePlacesAutocomplete功能异常求助
React Native Expo构建APK后Google Places Autocomplete失效问题
我在React Native项目中集成了Google Places API,使用GooglePlacesAutocomplete实现地点搜索查询。该功能在Expo测试环境下可正常使用,但构建测试APK安装到真实设备后,无结果返回,GooglePlacesAutocomplete功能失效。开发环境一切正常,难以排查此问题,恳请帮助。
实现代码
return ( <SafeAreaView style={{ backgroundColor: 'white', height: "100%" }}> <View> <Text style={{ color: 'black', fontWeight:'bold', fontSize: 25, padding: 10 }}>Lets Move it!</Text> <GooglePlacesAutocomplete placeholder='Move from?' styles={{ container: { flex: 0, }, textInput: { fontSize: 18, backgroundColor: 'white', borderWidth: 1, padding: 8, margin: 8, borderColor: '#a29e9e11' } }} onPress={(data, details = null) => { dispatch(setOrigin({ location: details.geometry.location, description: data.description })) dispatch(setDestination(null)) }} fetchDetails={true} enablePoweredByContainer={false} query={{ key: GOOGLE_MAPS_APIKEY, language: 'en'}} nearbyPlacesAPI='GooglePlacesSearch' debounce={400} /> </View> <View style={{ flex:1 }}> <BottomView /> </View> </SafeAreaView> )
排查方案
- 检查API密钥的访问限制
开发环境(Expo Go)使用的API密钥可能允许调试地址,但打包APK后,必须在Google Cloud控制台的API密钥配置中添加APK的应用包名和SHA-1签名指纹(包括调试和发布版本的指纹),否则API请求会被拒绝。 - 确认网络权限配置
检查app.json或app.config.js的Android配置中是否包含网络权限:
虽然Expo默认会添加该权限,但如果手动修改过配置可能遗漏。"android": { "permissions": ["INTERNET"] } - 验证API服务是否启用
登录Google Cloud控制台,确认Places API和相关服务(如Geocoding API)处于启用状态,避免因服务被误关闭导致请求失败。 - 检查环境变量传递
如果GOOGLE_MAPS_APIKEY是通过环境变量注入的,确保打包时环境变量已正确传递到APK中。可以在app.config.js中明确引用环境变量,或者确认打包命令已加载对应的环境配置。 - 查看真机错误日志
通过adb logcat *:E命令连接真机查看错误日志,Google Places API调用失败通常会返回具体错误信息(如密钥无效、权限不足等),这是定位问题最直接的方式。 - 检查组件配置合理性
确认nearbyPlacesAPI='GooglePlacesSearch'是否需要额外的API权限或服务支持,同时可以尝试调整debounce值,但这一般不会导致完全无结果的情况。
内容的提问来源于stack exchange,提问作者Eric
相关产品推荐
相关产品推荐

