Exception:Google Maps SDK for iOS must be initialized via [GMSServices provideAPIKey:…] prior to use

前端 未结 10 1860
挽巷
挽巷 2021-01-04 12:44

I am trying to implement Google Maps SDK into my project using Swift 2.0. I follow this but when running this, my app is getting the following error:

2015-08         


        
相关标签:
10条回答
  • 2021-01-04 13:21

    XCODE 12.0.1, Swift 5

    I was applying constraints to Google Map View by initialising GMSMapView like this:-

    private var googleMapsView : GMSMapView = {
        let mainView = GMSMapView()
        mainView.translatesAutoresizingMaskIntoConstraints = false
        mainView.isMyLocationEnabled = true
        return mainView
    }()
    

    & I was getting this same error, so I tried this making it a lazy property:-

    lazy private var googleMapsView : GMSMapView = {
        let mainView = GMSMapView()
        mainView.translatesAutoresizingMaskIntoConstraints = false
        mainView.isMyLocationEnabled = true
        return mainView
    }()
    

    & voila! It worked like a charm.

    0 讨论(0)
  • 2021-01-04 13:22

    Just adding to Rajasekaran Gopal

    Remember to add

    import GoogleMaps
    import GooglePlaces
    

    then

    GMSPlacesClient.provideAPIKey("Your key")
    GMSServices.provideAPIKey("Your key")
    

    in didFinishLaunchingWithOptions

    0 讨论(0)
  • 2021-01-04 13:22

    Here is a Google Maps tutorial for Swift:

    http://www.appcoda.com/google-maps-api-tutorial/

    Below is quoted from Google Maps Documentation:

    Step 5: Get an iOS API key

    Using an API key enables you to monitor your application's API usage, and ensures that Google can contact you about your application if necessary. The key is free, you can use it with any of your applications that call the Google Maps SDK for iOS, and it supports an unlimited number of users. You obtain an API key from the Google Developers Console by providing your application's bundle identifier.

    If your project doesn't already have a key for iOS applications, follow these steps to create an API key from the Google Developers Console:

    1. In the sidebar on the left, select Credentials.
    2. If your project doesn't already have an iOS API key, create one now by selecting Add credentials > API key > iOS key.
    3. In the resulting dialog, enter your app's bundle identifier. For example: com.example.hellomap.
    4. Click Create.

      Your new iOS API key appears in the list of API keys for your project. An API key is a string of characters, something like this:

      AIzaSyBdVl-cTICSwYKrZ95SuvNw7dbMuDt1KG0

    5. Add your API key to your AppDelegate.m as follows:

      • Add the following import statement:

      @import GoogleMaps;

      • Add the following to your application:didFinishLaunchingWithOptions: method, replacing API_KEY with your API key:

      [GMSServices provideAPIKey:@"API_KEY"];

    Source

    0 讨论(0)
  • 2021-01-04 13:34

    You are skipping provideAPIKey in your AppDelegate check project

    Your_PROJECT -> ios -> Runner -> AppDelegate.m

    Replace your file code with below snippet add your APIKEY

    #include "AppDelegate.h"
    #include "GeneratedPluginRegistrant.h"
    #import "GoogleMaps/GoogleMaps.h"
    
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application
        didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      [GMSServices provideAPIKey:@"GOOGLE_API_KEY"];
      [GeneratedPluginRegistrant registerWithRegistry:self];
      return [super application:application didFinishLaunchingWithOptions:launchOptions];
    }
    @end
    

    Google Maps SDK for iOS must be initialized via [GMSServices provideAPIKey:...]

    0 讨论(0)
  • 2021-01-04 13:35

    If you already set GMSMapView class to your view in interface builder, make GMSServices.provideAPIKey("API key") in initialization methods of viewController which contains GMSMapView.

    0 讨论(0)
  • 2021-01-04 13:39

    I just had the same problem. I created a GMSMapView object and it was initialized before maybe the api key could be read. So I moved it inside the viewDidLoad method and problem solved.

    Before :

    class ViewController: ..... {
    
    let mapView = GMSMapView()
    

    After :

    class ViewController: ..... {
    
    var mapView : GMSMapView?
    
    override viewDidLoad(){
        mapView = GMSMapView()
    
    0 讨论(0)
提交回复
热议问题