0

I am trying to work with google maps and everything was working fine until i add these two functions for getting updated Location.

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus)
    {
        if (status == CLAuthorizationStatus.AuthorizedWhenInUse)
        {
            vwGMap.myLocationEnabled = true
        }
    }


func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
    let newLocation = locations.last
    vwGMap.camera = GMSCameraPosition.cameraWithTarget(newLocation!.coordinate, zoom: 15.0)
    vwGMap.settings.myLocationButton = true
    self.view = self.vwGMap
}

Now, when i am trying to execute my app, there is an crash with exception "Terminating app due to uncaught exception 'GMSServicesException', reason: 'Google Maps SDK for iOS must be initialized via [GMSServices provideAPIKey:...] prior to use"

My API key is valid and already placed in Appdelegate. Anyone, Has any Idea what i am doing wrong here?

I am also presenting my viewDidLoad Code of my ViewController

let camera = GMSCameraPosition.cameraWithLatitude(-33.8683,
                                                          longitude: 151.2086, zoom: 15)
        let mapView = GMSMapView.mapWithFrame(self.view.bounds, camera: camera)


        mapView.setMinZoom(8, maxZoom: 18)
        mapView.indoorEnabled = false
        mapView.settings.compassButton = true
        mapView.settings.myLocationButton = true
        mapView.myLocationEnabled = true
        mapView.camera = camera

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
        locationManager.distanceFilter = 500
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()



        self.view = mapView
Himanshu
  • 2,832
  • 4
  • 23
  • 51
  • check this [SO question](http://stackoverflow.com/questions/32205872/exceptiongoogle-maps-sdk-for-ios-must-be-initialized-via-gmsservices-provideap) and this [issue](https://code.google.com/p/gmaps-api-issues/issues/detail?id=8370) if it can help you. – KENdi May 05 '16 at 08:17

1 Answers1

2

This may happen if some of your Google Maps code runs before API key is initialized.

For example if your vwGMap variable is an instance property:

let vwGMap = GMSMapView()

It could be instantiated during construction of your class, which could happen before provideAPIKey call. If so, you need to make sure, that it initialized after provideAPIKey call, by moving initialization into some method. It concerns all similar Google Maps properties.

Vladimir K
  • 1,382
  • 1
  • 12
  • 27
  • No help from this too. Same error is thrown by the app! – Himanshu May 04 '16 at 05:48
  • It won't do any help to me. I just removed these two functions and my app is running smooth. Do you know how to get updated location from google maps. – Himanshu May 04 '16 at 06:07
  • any solution please? – nr5 Nov 08 '17 at 11:04
  • @VladimirK are you able to elaborate on your proposed solution? I've tried so many options and can't get it to work. My viewcontroller class is loading before appdelegate so causing the same issue. I've tried adding the apikey into the viewcontroller class but still get the same error. – Anthony Jun 24 '19 at 08:32