Search code examples
iosxcodegoogle-mapsswift2google-maps-sdk-ios

Google Maps IOS SDK unexpected nil when unwrapping optional when calling for current location


I'm experiencing a problem while using the Google Maps IOS SDK. When I try calling for a current location, I get the error "unexpected nil when unwrapping optional". It seems to be that I am getting nil when referencing the newLocation variable. Any help will be greatly appreciated.

import UIKit;
import GoogleMaps;
import CoreLocation;


class GoHomeController: UIViewController, CLLocationManagerDelegate{
@IBOutlet weak var mapView: GMSMapView! //View holding google map in main.storyboard

private var locationManager = CLLocationManager();

override func viewDidLoad() {
    super.viewDidLoad();
    let mapview = GMSMapView.mapWithFrame(CGRectZero, camera: GMSCameraPosition.cameraWithLatitude(35, longitude: -65, zoom: 10.0));
    locationManager.delegate = self;
    locationManager.requestAlwaysAuthorization();
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.startUpdatingLocation();
    mapview.myLocationEnabled = true;
    mapView = mapview;


}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
    print("didFailWithError: \(error.description)");
    let errorAlert = UIAlertView(title: "Error", message: "Failed to Get Your Location", delegate: nil,cancelButtonTitle: "Ok");
    errorAlert.show();
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let newLocation = locations.last as CLLocation!
    //print("current position: \(newLocation.coordinate.longitude) , \(newLocation.coordinate.latitude)")
    if let location = newLocation{
    updateMapFrame(location, zoom: 10.0);
    }
    }
func updateMapFrame(newLocation: CLLocation, zoom: Float) {
    let camera = GMSCameraPosition.cameraWithTarget(newLocation.coordinate, zoom: zoom)
    self.mapView.animateToCameraPosition(camera) // Error occurs here
}


}

Thank you all for your time.

EDIT: I forgot to mention that GoHomeController is just a view controller in my project. It isn't the main one I have a segue leading to it.

EDIT #2: the error occurs after launching the app in the simulator, the moment I segue to the go home scene.


Solution

  • It is a well known issue. In Xcode to Edit Scheme -> Disable Metal Api will fix the issue.