初始化GMSMapView崩溃

时间:2018-01-30 16:06:16

标签: swift google-maps

enter image description here enter image description here我的程序在初始化GMSMapView对象时崩溃。 例如:

class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDelegate {
var window: UIWindow?
let store = Store.default
let map: GMSMapView = GMSMapView()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    guard GMSServices.provideAPIKey(AppConstants.Keys.googleMaps.rawValue) else { fatalError("Can not initialize Google Map services") }

    print("Map: \(self.map)")

    return true
}

在初始化GMSMapView时,代码会报告以下内容:

Thread 1: signal SIGABRT
libc++abi.dylib: terminating with uncaught exception of type NSException

2 个答案:

答案 0 :(得分:0)

class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDelegate {
var window: UIWindow?
let store = Store.default
var map: GMSMapView?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    guard GMSServices.provideAPIKey(AppConstants.Keys.googleMaps.rawValue) else { fatalError("Can not initialize Google Map services") }
    self.map = GMSMapView()
    print("Map: \(self.map)")

    return true
}

尝试像这样改变它

答案 1 :(得分:0)

检查控制台输出。您应该看到以下错误: Terminating app due to uncaught exception 'GMSServicesException', reason: 'Google Maps SDK for iOS must be initialized via [GMSServices provideAPIKey:...] prior to use

您正尝试在GMSService.provideAPIKey

之前初始化mapView