我正在尝试在我的应用程序中添加谷歌地图。我正在使用google_maps_flutter版本:0.5.21+7。我使用了来自谷歌代码实验室的以下代码。这是代码
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
GoogleMapController mapController;
final LatLng _center = const LatLng(45.521563, -122.677433);
void _onMapCreated(GoogleMapController controller) {
mapController = controller;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Maps Sample App'),
backgroundColor: Colors.green[700],
),
body: GoogleMap(
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(
target: _center,
zoom: 11.0,
),
),
),
);
}
}
但是代码不起作用,地图没有显示。这是我在 android 模拟器上运行时得到的错误报告,并且应用程序在真实设备上崩溃。
/ (23740): HostConnection::get() New Host Connection established 0xcba7f740, tid 23754
I/zzbz (23740): Making Creator dynamically
W/mple.test_mapp(23740): Unsupported class loader
W/mple.test_mapp(23740): Skipping duplicate class check due to unsupported classloader
D/EGL_emulation(23740): eglMakeCurrent: 0xded8e8a0: ver 3 0 (tinfo 0xded203c0)
I/DynamiteModule(23740): Considering local module com.google.android.gms.maps_dynamite:0 and remote module com.google.android.gms.maps_dynamite:221
I/DynamiteModule(23740): Selected remote version of com.google.android.gms.maps_dynamite, version >= 221
V/DynamiteModule(23740): Dynamite loader version >= 2, using loadModule2NoCrashUtils
W/mple.test_mapp(23740): Unsupported class loader
W/mple.test_mapp(23740): Skipping duplicate class check due to unsupported classloader
I/Google Maps Android API(23740): Google Play services client version: 12451000
I/Google Maps Android API(23740): Google Play services package version: 19275040
W/mple.test_mapp(23740): Accessing hidden field Ljava/nio/Buffer;->address:J (light greylist, reflection)
E/GoogleMapController(23740): Cannot enable MyLocation layer as location permissions are not granted
I/mple.test_mapp(23740): Background concurrent copying GC freed 7812(1108KB) AllocSpace objects, 0(0B) LOS objects, 49% free, 1783KB/3MB, paused 5.572ms total 32.898ms
D/ (23740): HostConnection::get() New Host Connection established 0xc05b5f40, tid 23799
D/EGL_emulation(23740): eglCreateContext: 0xbf4da240: maj 1 min 0 rcv 1
D/EGL_emulation(23740): eglMakeCurrent: 0xe0785600: ver 3 0 (tinfo 0xc807bd60)
D/EGL_emulation(23740): eglMakeCurrent: 0xbf4da240: ver 1 0 (tinfo 0xbf4c6160)
D/EGL_emulation(23740): eglMakeCurrent: 0xe0785600: ver 3 0 (tinfo 0xc807bd60)
D/ (23740): HostConnection::get() New Host Connection established 0xc05d0700, tid 23752
E/Google Maps Android API(23740): Authorization failure. Please see https://developers.google.com/maps/documentation/android-api/start for how to correctly set up the map.
E/Google Maps Android API(23740): In the Google Developer Console (https://console.developers.google.com)
E/Google Maps Android API(23740): Ensure that the "Google Maps Android API v2" is enabled.
E/Google Maps Android API(23740): Ensure that the following Android Key exists:
E/Google Maps Android API(23740): Android Application (<cert_fingerprint>;<package_name>): 0E:5D:3B:4B:C1:50:A0:16:C4:FF:FB:C1:2B:C1:AC:D3:15:25:D3:F6;com.example.test_mapp2
D/EGL_emulation(23740): eglMakeCurrent: 0xe0785600: ver 3 0 (tinfo 0xc807bd60)
D/EGL_emulation(23740): eglMakeCurrent: 0xe0785600: ver 3 0 (tinfo 0xc807bd60)
W/DynamiteModule(23740): Local module descriptor class for com.google.android.gms.googlecertificates not found.
I/DynamiteModule(23740): Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:4
I/DynamiteModule(23740): Selected remote version of com.google.android.gms.googlecertificates, version >= 4
W/mple.test_mapp(23740): Unsupported class loader
W/mple.test_mapp(23740): Skipping duplicate class check due to unsupported classloader
此错误日志中E/Google Maps Android API(23740): Authorization failure. Please see https://developers.google.com/maps/documentation/android-api/start for how to correctly set up the map.
您遇到 API 授权失败。
我将在下面留下一些有关如何解决此问题的链接: 地图安卓API:授权失败SO答案