Harsha
Harsha

Reputation: 818

Google Maps iOS SDK

This is my code

import UIKit    
import GooglePlaces  
import GoogleMaps  
@UIApplicationMain  

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

   GMSPlacesClient.provideAPIKey("************************")
   GMSServices.provideAPIKey("************************")

    return true
}

but I'm still getting the exception

Terminating app due to uncaught exception 'GMSServicesException', reason: 'Google Maps SDK for iOS must be initialized via [GMSServices provideAPIKey:...] prior to use'

Is there any other cause, help me to fix it.

Upvotes: 0

Views: 1627

Answers (2)

swiftBoy
swiftBoy

Reputation: 35783

You must add API Key in App delegate and implement GoogleMap delegate in ViewController

import UIKit
import GoogleMaps

let googleApiKey = "<YOUR API Key HERE>"

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        GMSServices.provideAPIKey(googleApiKey)
        return true
    }
//other methods
}

and your ViewController should be like

import UIKit
import GoogleMaps

class ViewController: UIViewController, CLLocationManagerDelegate {

    private let locationManager = CLLocationManager()
    @IBOutlet weak var mapView: GMSMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
    }

    // MARK: - CLLocationManagerDelegate
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {

        guard status == .authorizedWhenInUse else {
            return
        }
        locationManager.startUpdatingLocation()
        mapView.isMyLocationEnabled = true
        mapView.settings.myLocationButton = true
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        guard let location = locations.first else {
            return
        }

        mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
        locationManager.stopUpdatingLocation()
    }
}

PS: Make sure you don't forget these important steps.

  1. Go to Google Developers Console and create app and API Key
  2. Enable APIs for Google Maps SDK for iOS
  3. Add privacy permissions in .Plist file

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>Application requires user’s location information while the app is running in the foreground.</string>
    <key>NSLocationAlwaysUsageDescription</key>
    <string>Will you allow this app to always know your location?</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>Do you allow this app to know your current location?</string>

Upvotes: 0

Harsha
Harsha

Reputation: 818

ViewController class is calling before appDelegate. So, the APIKey was not initiated. After finding this I initiate the viewController in appDelegate.

Upvotes: 0

Related Questions