0

I am attempting to center my google maps view on the users current location however I am having trouble due to the placesClient being equal to nil although I assign the gpsCoordinates to it, below is my code inside of my viewdidload() of the view controller

var gmsPlace : GMSPlace?
var gpsCoordinates : CLLocationCoordinate2D?

override func viewDidLoad() {
    super.viewDidLoad()
    //placesClient = GMSPlacesClient()
    var placesClient = GMSPlacesClient.sharedClient()
    placesClient.currentPlaceWithCallback { (placeLikelihoods, error) -> Void in
        guard error == nil else {
            print("Current Place error: \(error!.localizedDescription)")
            return
        }
        var gmsPlace : GMSPlace?

        if let placeLikelihoods = placeLikelihoods {
            for likelihood in placeLikelihoods.likelihoods {
                gmsPlace = likelihood.place
                //print("Current Place name \(gmsPlace.name) at likelihood \(likelihood.likelihood)")
                //print("Current Place address \(gmsPlace.formattedAddress)")
                //print("Current Place attributions \(gmsPlace.attributions)")
                //print("Current PlaceID \(gmsPlace.placeID)")
                self.gpsCoordinates = (gmsPlace!.coordinate)

            }
            print(self.gpsCoordinates)

        }
    }
    let testObject = PFObject(className: "TestObject")
    testObject["foo"] = "bar"
    testObject.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
        print("Object has been saved.")
    }
    let camera = GMSCameraPosition.cameraWithTarget(gpsCoordinates!, zoom: 16.9)
    //let camera = GMSCamera
    print(camera)
    viewMap.camera = camera
    viewMap.myLocationEnabled = true
    viewMap.settings.myLocationButton = true


    let marker = GMSMarker()
    marker.position = self.gpsCoordinates!
    marker.title = "Newport Beach"
    marker.snippet = "California"
    marker.map = viewMap

    // Do any additional setup after loading the view, typically from a nib.
    print(url)
    print(videoData)
    print(doUpload)
    print(FriendsOrPublic)
    print(dataPath)
    if doUpload == true {
        Upload()
    }

   // Download()
}
maz
  • 349
  • 2
  • 3
  • 15

2 Answers2

0

Hi there just do this in view will appear your problem will be solved

  • That did not fix the issue, the app still crashes with an optional nil value and does not load the map – maz Jun 20 '16 at 06:26
0

Basically, error encountered is caused by an Implicitly Unwrapped Optional returning a nil value.

As discussed in Optionals:

If you define an optional variable without providing a default value, the variable is automatically set to nil.

So, to prevent your apps from crashing, you need to unwrap an optional value which can be done safely or forcibly.

A very detailed explanation on the cause of this kind of error and how to deal with it is found in this SO post - What does “fatal error: unexpectedly found nil while unwrapping an Optional value” mean?.

Community
  • 1
  • 1
Teyam
  • 7,686
  • 3
  • 15
  • 22