-2

I'm trying to generate a map using the GoogleMaps SDK, I had previously generated a map using the lines "let map = GMSMapView()" and then setting the view with "self.view = map" and that worked like a charm, but then I switched over to this more complicated format used in a tutorial I am following, and even though the code is the same, mine appears to fail to generate the map :(

Any help would be amazing as I've been struggling with this for a while now and I really can't spot the issue!

//Modules
import GoogleMaps
import SwiftUI
import UIKit
import GoogleMapsUtils


class MapViewController: UIViewController, GMSMapViewDelegate, CLLocationManagerDelegate {
    var mapView: GMSMapView!
    
    let locationManager = CLLocationManager()
    
    override func loadView() {
        super.loadView() //LoadMap - Default
        
        locationManager.delegate = self
        locationManager.requestAlwaysAuthorization()
        
        guard let myLoc = locationManager.location?.coordinate else{return}
        
        if CLLocationManager.locationServicesEnabled() {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.startUpdatingLocation()
        }
        
        //Camera Focus
        let camera = GMSCameraPosition.camera(withLatitude: myLoc.latitude, longitude: myLoc.longitude, zoom: 15.0)
        mapView = GMSMapView.map(withFrame: self.view.bounds, camera: camera)
        mapView.isMyLocationEnabled = true
        mapView.delegate = self
        self.view = mapView
        }
}

EDIT: Added swiftui bridge viewcontroller below:

import GoogleMaps
import SwiftUI

struct MapViewControllerBridge: UIViewControllerRepresentable {
    
    func makeUIViewController(context: Context) -> MapViewController {
        let uiViewController = MapViewController()
        return uiViewController
    }
    
    func updateUIViewController(_ uiViewController: MapViewController, context: Context) {
    }
}
Skyrifter
  • 1
  • 1
  • If you think it is related to SwiftUI then add a corresponding SwiftUI code which you think result in issue. Otherwise it should not include [swiftui] tag, even though you have SwiftUI code in your project, because provided code and description has nothing about SwiftUI. Invest your time reading [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Asperi Jul 08 '22 at 06:48
  • @Asperi I've now added the swiftui code below, sorry and I hope that helps :) – Skyrifter Jul 08 '22 at 15:26

1 Answers1

0

you have just assigned the view. instead of that add mapView to view as subview just replace this line self.view = mapView with

self.view.addSubview(mapView)

ABIRAMI R
  • 11
  • 1