Jonathan Allen Grant
Jonathan Allen Grant

Reputation: 3648

Swift: My GMSMapView is unexpectedly nil

I am trying to follow a simple Google Maps tutorial. It basically gets an address from the user, changes the address into latitude and longitude coordinates based off the google maps api, and then moves the on screen map view to those coordinates. Every step above works except the last. I have set up breakpoints to see what is causing my Thread 1 error: EXC_BAD_INSTRUCTION.

According to Xcode, my Google Maps View and Camera is nil at runtime. Here is my code to instantiate my Google Maps View:

@IBOutlet weak var mapsView: GMSMapView! = GMSMapView.mapWithFrame(CGRectZero, camera: GMSCameraPosition.cameraWithLatitude(-33.86,
    longitude: 151.20, zoom: 6))

My maps view works before I attempt to change addresses, so I do not think it should be nil. Here is the code I run when I change addresses:

                    let coordinate = CLLocationCoordinate2D(latitude: self.mapTasks.fetchedAddressLatitude, longitude: self.mapTasks.fetchedAddressLongitude)
                let camera = GMSCameraPosition.cameraWithTarget(coordinate, zoom: 14.0)
                self.mapsView.camera = camera

Through breakpoints I know that the latitude and longitude are legit, and that my camera and Maps View are nil. Why is this?

Upvotes: 1

Views: 1434

Answers (1)

unknowncoder
unknowncoder

Reputation: 132

Do you remove your IBOutlet? If so, try this :

@IBOutlet weak var mapsView: GMSMapView!

let latitude = self.mapTasks.fetchedAddressLatitude
let longitude = self.mapTasks.fetchedAddressLongitude
let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let camera = GMSCameraPosition.cameraWithTarget(coordinate, zoom: 14.0)

self.mapsView.camera = camera

Hope it's work! :)

Upvotes: 3

Related Questions