我有这段代码:
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
和这段代码:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation = locations.last
print(userLocation)
let camera = GMSCameraPosition.cameraWithLatitude(userLocation!.coordinate.latitude,
longitude: userLocation!.coordinate.longitude, zoom: 12.5)
self.mapView.camera = camera
self.mapView.myLocationEnabled = true
// self.mapView.settings.myLocationButton = true
locationManager.stopUpdatingLocation()
}
这比较早,但现在似乎没有调用locationManager,因为print语句根本没有返回任何信息。代码中没有错误,我似乎无法弄清楚为什么函数没有被调用。任何人都可以发现任何我不知道的明显事物吗?
谢谢
答案 0 :(得分:0)
我刚刚实施了这个,我会告诉你我做了什么
首先,创建一个locationManager变量
let locationManager: CLLocationManager = CLLocationManager()
您必须在viewDidLoad()
viewMap.delegate = self
locationManager.delegate = self as? CLLocationManagerDelegate
locationManager.requestWhenInUseAuthorization()
然后,我创建了一个扩展,并实现了委托
extension MyView: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
//here you configure the locationManager
locationManager.startUpdatingLocation()
viewMap.isMyLocationEnabled = true
viewMap.settings.myLocationButton = true
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
}
}
我希望这对你有帮助,这里运作正常。