Flutter集成Google Maps API遇空白页及400错误求助
Flutter集成Google Maps API后空白且报400错误求助
我在Flutter应用中集成Google Maps API后,页面始终显示空白,且报错:Unexpected response code 400 for https://clients4.google.com/glm/mmap/api。我已在所有可能位置配置API密钥,创建了数十个新API密钥与项目,反复启用禁用API,执行了15次flutter clean,通读两遍官方文档,参考Stack Overflow相关问题并尝试所有解决方案,但均无效,恳请帮助。
iOS代码
import UIKit import Flutter import GoogleMaps @UIApplicationMain @objc class AppDelegate: FlutterAppDelegate { override func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { GMSServices.provideAPIKey("KEY HERE") GeneratedPluginRegistrant.register(with: self) return super.application(application, didFinishLaunchingWithOptions: launchOptions) } }
Android代码
<meta-data android:name="com.google.android.geo.API_KEY" android:value="KEY HERE"/> // // minSdkVersion 20
Maps.dart代码
class Mapp extends StatefulWidget { const Mapp({Key? key}) : super(key: key); static final kInitialPosition = LatLng(-33.8567844, 151.213108); @override _MappState createState() => _MappState(); } class _MappState extends State<Mapp> { PickResult? selectedPlace; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("Google Map Place Picker"), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: <Widget>[ ElevatedButton( child: Text("Load Google Map"), onPressed: () { Navigator.push( context, MaterialPageRoute( builder: (context) { return PlacePicker( apiKey: "KEY HERE", // APIKeys.apiKey, initialPosition: Mapp.kInitialPosition, useCurrentLocation: true, selectInitialPosition: true, //usePlaceDetailSearch: true, onPlacePicked: (result) { selectedPlace = result; Navigator.of(context).pop(); setState(() {}); }, ); }, ), ); }, ), selectedPlace == null ? Container() : Text(selectedPlace?.formattedAddress ?? ""), ], ), )); } }
排查建议
- 确认API权限与计费状态:检查是否启用了
Maps SDK for Android、Maps SDK for iOS以及Places API,同时确保谷歌账号已绑定支付方式(部分服务需启用计费才能正常调用,即使在免费额度内)。 - 检查密钥应用限制:API密钥的应用限制必须匹配实际项目,Android需设置正确的包名+SHA-1签名,iOS需设置Bundle ID,避免设置IP限制或错误的应用标识。
- 替换废弃插件:当前使用的
PlacePicker插件已被官方废弃,建议替换为google_maps_flutter结合google_maps_places的官方组合方案,避免插件内部过时的请求逻辑导致400错误。 - 验证网络连通性:确认设备能正常访问谷歌服务,可尝试直接访问
https://clients4.google.com/glm/mmap/api(虽可能返回403,但能连通则说明网络无问题)。 - 抓包分析请求参数:通过抓包工具查看实际请求的参数,确认API密钥是否正确携带,是否存在参数缺失或格式错误。
内容的提问来源于stack exchange,提问作者Tyrone
相关产品推荐
相关产品推荐

