设置官方Google Maps for Flutter插件的说明包括将Google API密钥添加到AppDelegate.m文件中:
在应用程序委托ios / Runner / AppDelegate.m中指定您的API密钥:
#include "AppDelegate.h" #include "GeneratedPluginRegistrant.h" #import "GoogleMaps/GoogleMaps.h" @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [GMSServices provideAPIKey:@"YOUR KEY HERE"]; [GeneratedPluginRegistrant registerWithRegistry:self]; return [super application:application didFinishLaunchingWithOptions:launchOptions]; } @end
我的flutter项目有一个AppDelegate.swift文件而不是AppDelegate.m文件,我不知道如何添加所需的密钥,因为语法不同:
import UIKit import Flutter @UIApplicationMain @objc class AppDelegate: FlutterAppDelegate { override func application( _ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]? ) -> Bool { GeneratedPluginRegistrant.register(with: self) return super.application(application,didFinishLaunchingWithOptions: launchOptions) } }
谁能帮我吗?
解决方法
您可以按如下方式添加API密钥:
AppDelegate.swift:
import UIKit import Flutter import GoogleMaps // Add this line! @UIApplicationMain @objc class AppDelegate: FlutterAppDelegate { override func application( _ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]? ) -> Bool { GeneratedPluginRegistrant.register(with: self) GMSServices.provideAPIKey("YOUR_API_KEY") // Add this line! return super.application(application,didFinishLaunchingWithOptions: launchOptions) } }