Cesa Salaam
Cesa Salaam

Reputation: 77

Why is my mapView (GMSMapView) nil?

can someone please help me? I'm segueing from one view (verifyCode) to another view (mainMap). When i segue to the mainMap i receive an error saying that the mapView is nil. Why is this happening?

Here's some of the code: mainMapController

@IBOutlet weak var mapView: GMSMapView!


override func viewDidLoad() {

    super.viewDidLoad()
    let camera = GMSCameraPosition.camera(withLatitude: 38.96351774, longitude:-77.01957901, zoom: 15.0)
    mapVIEW.camera = camera <-------- it errors on this line.
}

verifyCodeController :

if code == codeField.text{
        numberInUse(number: phoneNumber!, completionHandler: { (Value) in
            if Value == true{
                print("toMap segue")
                print(Value)
                self.performSegue(withIdentifier: "nextMap", sender: nil)
            }

Upvotes: 3

Views: 861

Answers (1)

Scriptable
Scriptable

Reputation: 19750

The Documentation shows how to initialise the map

// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
view = mapView

Theres nothing in your code to suggest you've actually initialised it

You at least need to call

let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)

UPDATE:

Make sure that you set your API key on App Startup:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    GMSServices.provideAPIKey("YOUR_API_KEY")
    return true
}

Upvotes: 3

Related Questions