(SwiftUI) Google Maps Works Fine When Hitting “Play” But Doesn't Work On Side Preview

你。 提交于 2020-07-23 08:02:11

问题


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

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

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'"

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

Here is rest of my code:

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
}

//App Delegate:

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


回答1:


Put it into PreviewProvider, like below

struct GoogleMapView_Previews: PreviewProvider {
    static var previews: some View {
        GMSServices.provideAPIKey("MyAPIKey")
        GoogleMapView()
    }
}



回答2:


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
}


来源:https://stackoverflow.com/questions/61552847/swiftui-google-maps-works-fine-when-hitting-play-but-doesnt-work-on-side-pr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!