Map center and add marker using Google Map SDK Subview (Swift)

I am having problems so that the Google map can center itself and add a marker when it is connected to the subview as an IBOutlet. I tried to get it to work, it feels like 20 different ways, but I'm stuck, so I would appreciate any help or guidance.

Setting up the code is pretty simple:

//Map view outlet
@IBOutlet weak var googleMapView: GMSMapView!

override func viewDidLoad() {
    super.viewDidLoad()

// Google Map View Setup
    let camera = GMSCameraPosition.cameraWithLatitude(-33.86, longitude: 151.20, zoom: 6)
    let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
    mapView.myLocationEnabled = true

    self.googleMapView = mapView

    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2DMake(-33.86, 151.20)
    marker.title = "Sydney"
    marker.snippet = "Australia"
    marker.map = mapView

I can make it work if the view is the entire view controller by changing self.googleMapView to self.view, but I cannot figure out how to properly connect it to the googleMapView output. Thank!

, , , mapView, .

+4
2

, , , , ! GMSMapView, . GMSMapView Outlet, .

:

 // Google Map View Setup
    let camera = GMSCameraPosition.cameraWithLatitude(-33.86, longitude: 151.20, zoom: 6)
    self.googleMapView.myLocationEnabled = true

    self.googleMapView.camera = camera

    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2DMake(-33.86, 151.20)
    marker.title = "Sydney"
    marker.snippet = "Australia"
    marker.map = googleMapView
+8

, mapView view viewDidLoad(). , mapView  func viewDidAppear (: Bool). , google self.view . , func viewDidLoad() .

override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        googleMapsView = GMSMapView()
        googleMapsView?.frame = mapView.bounds
        googleMapsView?.myLocationEnabled = true
        if let googleMapView = googleMapsView {
            mapView.addSubview(googleMapView)
        }
  }
+1

All Articles