iosxcodegoogle-mapsswift3

Google Maps API full screen in SWIFT3


This is the current code, after making the edits suggested, it no longer shows a marker on the map for Sydney Australia:

class LocateVC: UIViewController {

    @IBOutlet weak var mapView: GMSMapView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //override func loadView() {
        // 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: self.mapView.bounds, camera: camera)
        self.mapView = mapView
        
        // Creates a marker in the center of the map.
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
        marker.title = "Sydney"
        marker.snippet = "Australia"
        marker.map = mapView
      }
  }

Output

This is the screen that loads:

This is the screen that loads

And no marker on Sydney Australia:

And no marker on Sydney Australia


Solution

  • Your are setting mapview to main view of ViewController

    So Change your these two lines

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

    With:

    let mapView = GMSMapView.map(withFrame: self.mapView.bounds, camera: camera)
    self.mapView = mapView