使用斯威夫特3。所以問題是初始化變量GMSMapView
和CLLocationManager
。這是如何爲我工作:
我定義的ViewController
類變量如下
private var locationManager: CLLocationManager!
private var googleMapView: GMSMapView!
而在viewDidLoad()
我介紹了一個DispatchQueue
在主線程中運行它。
DispatchQueue.main.async {
//adding mapView
self.googleMapView = GMSMapView()
self.view.addSubview(self.googleMapView)
self.googleMapView.translatesAutoresizingMaskIntoConstraints = false
//auto layout constraints
self.googleMapView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0).isActive = true
self.googleMapView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: 0).isActive = true
self.googleMapView.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 0).isActive = true
self.googleMapView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0).isActive = true
//location manager setup
self.locationManager = CLLocationManager()
self.locationManager.delegate = self
self.locationManager.startUpdatingHeading()
}
@約翰Welliem ....請參考以下鏈接 https://www.3pillarglobal.com/insights/blocks-ios-introduction-to-grand-central-dispatch –
請同時參閱本 HTTPS ://www.appcoda.com/ios-concurrency/ –
一個更具體的答案將不勝感激。 –