将Google Maps添加为子视图可​​无限调用viewDidLoad()

时间:2019-05-10 10:05:23

标签: ios swift google-maps-sdk-ios

我正在尝试将Google Maps视图添加为UIViewController的子视图。但是它给了无限循环调用viewDidLoad()。该如何解决?

override func viewDidLoad() {
    super.viewDidLoad()
    let camera = GMSCameraPosition.camera(withLatitude: 53.9, longitude: 27.5667, zoom: 6)
    mapView = GMSMapView.map(withFrame: CGRect(x: 100, y: 100, width: 200, height: 200), camera: camera)
    mapView?.center = self.view.center
    self.view.addSubview(mapView!)
}

xcode callstack

2 个答案:

答案 0 :(得分:0)

我正在以编程方式创建UIViewController并添加以下代码解决了该问题。

override func loadView() {
    let screenSize = UIScreen.main.bounds
    view = UIView(frame: CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height))
    view.backgroundColor = UIColor.white
}

答案 1 :(得分:0)

 override func loadView() {
//Call the map
let camera = GMSCameraPosition.camera(withLatitude: "your latitude", longitude: "longitute", zoom: 15.0)
mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mapView.delegate = self
self.view = mapView
 mapView.settings.myLocationButton = true // enable current location button on map 
    mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    mapView.isMyLocationEnabled = true
}