(SwiftUI)在点击“播放"时,Google Maps可以正常运行但是在侧面预览中不起作用 [英] (SwiftUI) Google Maps Works Fine When Hitting "Play" But Doesn't Work On Side Preview

查看:99
本文介绍了(SwiftUI)在点击“播放"时,Google Maps可以正常运行但是在侧面预览中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我按下播放"按钮时,电话模拟器会弹出,并显示带有我的注释的google地图.

When I hit the "play" button, the phone emulator pops up and it shows google maps with my annotations.

但是,它不会显示在SwiftUI的侧面预览中.

However, it will not show on the side preview of SwiftUI.

我收到错误消息:由于未捕获的异常'GMSServicesException',正在终止应用程序,原因:'iOS的Google Maps SDK必须在使用前通过[GMSServices offerAPIKey:...]进行初始化''

I get the error: "Terminating app due to uncaught exception 'GMSServicesException', reason: 'Google Maps SDK for iOS must be initialized via [GMSServices provideAPIKey:...] prior to use'"

我一直在关注Google Maps入门?".并且已经提供了密钥,但是我仍然遇到此错误.

I have followed Google Maps "Getting Started" and have have provided the key but I'm still experiencing this error.

这是我的其余代码:

struct GoogleMapView: UIViewRepresentable {

let marker : GMSMarker = GMSMarker()

func makeUIView(context: Self.Context) -> GMSMapView {
    let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
    let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
    mapView.settings.zoomGestures = true

    do {

      if let styleURL = Bundle.main.url(forResource: "style", withExtension: "json") {
        mapView.mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL)
      } else {
        NSLog("Unable to find style.json")
      }
    } catch {
      NSLog("One or more of the map styles failed to load. \(error)")
    }


    return mapView
}

func updateUIView(_ mapView: GMSMapView, context: Self.Context) {

    marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
    marker.title = "Sydney"
    marker.snippet = "Australia"
    marker.map = mapView
}

//应用代表:

class AppDelegate: UIResponder, UIApplicationDelegate {


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

// MARK: UISceneSession Lifecycle

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
    // Called when a new scene session is being created.
    // Use this method to select a configuration to create the new scene with.
    return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}

func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
    // Called when the user discards a scene session.
    // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
    // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}

我该如何解决侧面预览的问题?谢谢

What can I do to fix the side preview? Thanks

推荐答案

尝试将API密钥放入makeUIView中,如下所示:

Try putting API Key in makeUIView, like below:

func makeUIView(context: Self.Context) -> GMSMapView {
    GMSServices.provideAPIKey("MyAPIKey")
    let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
    let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
    mapView.settings.zoomGestures = true

    do {

      if let styleURL = Bundle.main.url(forResource: "style", withExtension: "json") {
        mapView.mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL)
      } else {
        NSLog("Unable to find style.json")
      }
     } catch {
       NSLog("One or more of the map styles failed to load. \(error)")
    }


    return mapView
}

这篇关于(SwiftUI)在点击“播放"时,Google Maps可以正常运行但是在侧面预览中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆