Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps for Flutter iOS Swift setup

The instructions for setting up the official Google Maps for Flutter plugin include adding the Google API key to the AppDelegate.m file:

Specify your API key in the application delegate ios/Runner/AppDelegate.m:

#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

My flutter project has an AppDelegate.swift file instead of an AppDelegate.m file and I'm not sure how to add the required key, as the syntax is different:

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)
  }
}

Can anyone help me out?

like image 738
Kretin Avatar asked Dec 15 '18 04:12

Kretin


2 Answers

You can add your API key as follows:

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)
  }
}
like image 123
M Reza Avatar answered Oct 24 '22 08:10

M Reza


One more thing, Dont forget to add below line on ios/Runner/Info.plist

<key>io.flutter.embedded_views_preview</key>
<true/>
like image 10
Navin Kumar Avatar answered Oct 24 '22 08:10

Navin Kumar