0

I am aware that there are many other questions like this however I assure you this is not a duplicate as far as I can tell. As you can see in the code below I have not marked any values as Optional however I keep getting this error. It crashes when I run the snapNext value highlighting the

 viewMap.camera = newLocation

Here is the full code below, I have the viewMap linked to just a regular UIView

import UIKit
import MapKit
import GoogleMaps

class ViewController: UIViewController {

var camera = GMSCameraPosition.cameraWithLatitude(33.600727, longitude: -117.900840, zoom: 16.9)
@IBOutlet weak var viewMap: GMSMapView!
override func viewDidLoad() {
    super.viewDidLoad()


    viewMap.camera = camera
    viewMap = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
    viewMap.myLocationEnabled = true
    viewMap.settings.myLocationButton = true


    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2DMake(33.600727, -117.900840)
    marker.title = "Newport Beach"
    marker.snippet = "California"
    marker.map = viewMap

    // Do any additional setup after loading the view, typically from a nib.
}

@IBAction func snapNext(sender: AnyObject) {
    let newLocation = GMSCameraPosition.cameraWithLatitude(33.622578, longitude: -117.911099, zoom: 16.9)
    viewMap.camera = newLocation

   }
   override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}
maz
  • 349
  • 2
  • 3
  • 15

3 Answers3

1

Your error is in the line:

viewMap = GMSMapView.mapWithFrame(CGRectZero, camera: camera)

Here you are setting the map to a new anonymous object and because the map property is weak it will be set to nil automatically as soon as the anonymous object goes out of scope (i.e. when viewDidLoad completes). Either you want to create the object in the storyboard; in which case leave it as a weak outlet, or you just want it as a 'normal' property; in which case remove the weak and IBOutlet.

sgib
  • 1,030
  • 11
  • 7
0
import UIKit
import MapKit
import GoogleMaps

class ViewController: UIViewControllerGMSMapViewDelegate , CLLocationManagerDelegate {

var camera:GMSCameraPosition!

@IBOutlet weak var viewMap: GMSMapView!
override func viewDidLoad() {
    super.viewDidLoad()

    camera = GMSCameraPosition.cameraWithLatitude(33.600727, longitude: -117.900840, zoom: 16.9)

    viewMap.camera = camera
    viewMap = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
    viewMap.myLocationEnabled = true
    viewMap.delegate=self

    viewMap.settings.myLocationButton = true


    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2DMake(33.600727, -117.900840)
    marker.title = "Newport Beach"
    marker.snippet = "California"
    marker.map = viewMap

    // Do any additional setup after loading the view, typically from a nib.
}

@IBAction func snapNext(sender: AnyObject) {
    camera = GMSCameraPosition.cameraWithLatitude(33.622578, longitude: -117.911099, zoom: 16.9)
    viewMap.camera = camera

   }
   override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}
Jayesh Miruliya
  • 3,279
  • 2
  • 25
  • 22
  • I didn't think it was possible to add an "@IBAction" into the viewDidLoad()? It gives me an error and tells me to remove the "@IBAction" prefix form the function – maz May 03 '16 at 06:12
0

As you've already pointed out the viewMap is nil. Why it's getting released, I do not know (Need more info to know that). But I know that you can prevent it by removing the weak keyword.

@IBOutlet weak var viewMap: GMSMapView!

However, this might lead to leaks if you have retain cycles between your viewMap and your view controller.

ullstrm
  • 9,812
  • 7
  • 52
  • 83