Google maps SDK not working on SwiftUI app

195 Views Asked by At

I've followed the steps of adding a map to my iOS app and then created a struct to "bridge" between swiftUI and UIkit but for some reason when I call the struct nothing would show whatsoever.

This is my viewcontroller:

import CoreLocation
import UIKit
import GoogleMaps

class ViewController: UIViewController, CLLocationManagerDelegate{
    
    let manager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        manager.delegate = self
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()
        
        GMSServices.provideAPIKey("xxx")
        
        
        print("License: \n\n\(GMSServices.openSourceLicenseInfo())")
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let location = locations.first else {
            return
        }
        
        let coordinate = location .coordinate
        let camera = GMSCameraPosition.camera(withLatitude: coordinate.latitude, longitude: coordinate.longitude, zoom: 6.0)
        let mapView = GMSMapView.map(withFrame: view.frame, camera: camera)
        view.addSubview(mapView)

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

and this is the struct created in my content view:

struct YourViewControllerView: UIViewControllerRepresentable {

    func makeUIViewController(context: Context) -> ViewController {
        return ViewController()
    }

    func updateUIViewController(_ uiViewController: ViewController, context: Context) {
       
    }

}

I've searched for similar problems but everyone else had the problem of the map not loading (marker and google logo still show), whereas in my case nothing shows on the screen at all.

Just to reiterate, my Maps SDK for iOS is enabled.

I've been trying to find a solution for a week now so help is greatly appreciated.

0

There are 0 best solutions below