JaguarJmn
JaguarJmn

Reputation: 7

Resize Google Maps Swift 4

I have followed the Google maps get started tutorial, but the map goes under the status bar and I don't want that. So I created another view with the class property of GMSMapView and linked the created map to it. However, when I run the code I get:

terminating with uncaught exception of type NSException

and it goes to the AppDelegate.swift file saying:

Thread 1: signal SIGABRT

I don't know what I am doing wrong and couldn't find any answers to fix it.

Here is my swift 4 code:

import UIKit
import GoogleMaps

class FirstViewController: UIViewController {
    @IBOutlet weak var mapView: GMSMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        GMSServices.provideAPIKey("AIzaSyDNpN4_CfRBAY60U7qgWQIqJAwvjEFMwNk")
        let camera = GMSCameraPosition.camera(withLatitude: 41.3114, longitude: -105.5911, zoom: 15)
        mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)

        view.addSubview(mapView)
    }
}

Main.Storyboard picture

Upvotes: 0

Views: 280

Answers (1)

RajeshKumar R
RajeshKumar R

Reputation: 15778

When you have mapview in storyboard you don't need to initialize it again and add as subview.

And you should move set API key in AppDelegate

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        GMSServices.provideAPIKey("AIzaSyDNpN4_CfRBAY60U7qgWQIqJAwvjEFMwNk")
        return true
    }
}

ViewController

class ViewController: UIViewController{

    @IBOutlet weak var mapView: GMSMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        mapView.camera = GMSCameraPosition.camera(withLatitude: 41.3114, longitude: -105.5911, zoom: 15)
    }
}

Upvotes: 1

Related Questions