Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value - MapView.isMyLocationEnabled

27 Views Asked by At
self.MapView.isMyLocationEnabled = true

gives the following error. How do I fix it?

import GoogleMaps

class ViewController: UIViewController,CLLocationManagerDelegate {

    //Outlets
    @IBOutlet var MapView: GMSMapView!
    
    //Variables
    var locationManager = CLLocationManager()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        initializeTheLocationManager()
        self.MapView.isMyLocationEnabled = true
    }
    
    func initializeTheLocationManager() {
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    
        var location = locationManager.location?.coordinate
    
        cameraMoveToLocation(toLocation: location)
    
    }
    
    func cameraMoveToLocation(toLocation: CLLocationCoordinate2D?) {
        if toLocation != nil {
            MapView.camera = GMSCameraPosition.camera(withTarget: toLocation!, zoom: 15)
        }
    }

}

This is what the storyboard looks like. I am new to Swift and I want to see my current location on Google Maps via Swift. What should I do differently?

image

image

0

There are 0 best solutions below