CLLocationManager委托方法没有被调用(谷歌地图是集成的)

CLLocationManager的委托方法

didChangeAuthorizationStatus和didUpdateToLocation

没有被调用。

位置始终使用说明密钥已添加到info.plist中,并在第一次启动应用程序时收到通知。

我能够看到谷歌地图,但我无法看到当前的位置,当我改变位置,它没有得到更新。 Basicaaly委托方法没有被调用。

//码

import UIKit import GoogleMaps class ViewController: UIViewController,GMSMapViewDelegate { @IBOutlet weak var mapViewTest: GMSMapView! let locationManager = CLLocationManager() var currentLocation :CLLocation = CLLocation(latitude: 0.0, longitude: 0.0) var currentLatitude : Double = 0.0 var currentLongitude : Double = 0.0 override func viewDidLoad() { super.viewDidLoad()`` locationManager.delegate = self if (CLLocationManager.locationServicesEnabled()) { locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.allowsBackgroundLocationUpdates = true locationManager.requestAlwaysAuthorization() locationManager.startUpdatingLocation() } // Do any additional setup after loading the view, typically from a nib. } } extension ViewController : CLLocationManagerDelegate { func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) { if status == .authorizedAlways { if(CLLocationManager .locationServicesEnabled()) { locationManager.startUpdatingLocation() mapViewTest.isMyLocationEnabled = true mapViewTest.settings.myLocationButton = true } } } func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) { mapViewTest.camera = GMSCameraPosition(target: (newLocation.coordinate), zoom: 15, bearing: 0, viewingAngle: 0) currentLocation = newLocation currentLatitude = newLocation.coordinate.latitude currentLongitude = newLocation.coordinate.longitude } func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { print("Errors: " + error.localizedDescription) } } 

从你的代码中你正在使用Swift 3,而在Swift 3中CLLocationManagerDelegate方法的签名是这样改变的。

 func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { } //func locationManager(_ manager: CLLocationManager, didUpdateTo newLocation: CLLocation, from oldLocation: CLLocation) is deprecated with below one func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { } func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { } 

检查CLLocationManagerDelegate上的Apple文档以获取更多详细信息。

在检查你的代码后,我发现你需要做的一些改变,

注意:我只是在位置pipe理器中添加了有问题的代码

 import UIKit import CoreLocation class ViewController: UIViewController { let locationManager = CLLocationManager() var currentLocation :CLLocation = CLLocation(latitude: 0.0, longitude: 0.0) var currentLatitude : Double = 0.0 var currentLongitude : Double = 0.0 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. locationManager.delegate = self if (CLLocationManager.locationServicesEnabled()) { locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.allowsBackgroundLocationUpdates = true locationManager.requestAlwaysAuthorization() locationManager.startUpdatingLocation() } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } extension ViewController : CLLocationManagerDelegate { func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { } func locationManager(_ manager: CLLocationManager, didFinishDeferredUpdatesWithError error: Error?) { print("Errors: " + (error?.localizedDescription)!) } } 

如果没有添加,也在.plist文件中添加下面的行,

  <key>NSLocationWhenInUseUsageDescription</key> <string>$(PRODUCT_NAME) location use</string> <key>NSLocationAlwaysUsageDescription</key> <string>$(PRODUCT_NAME) always uses location </string> 

将这两个属性添加到您的info.plist中

 NSLocationWhenInUseUsageDescription NSLocationAlwaysUsageDescription 

你需要把mapview委托给自己。

尝试这个:

  override func viewDidLoad() { super.viewDidLoad() // User Location Settings locationManager.delegate = self locationManager.requestWhenInUseAuthorization() locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.startUpdatingLocation() // Google Maps Delegate mapView.delegate = self } // View will appear override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) // Google maps settings mapView.isMyLocationEnabled = true mapView.settings.myLocationButton = true // Get location if autorized if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse) { let (latitude, longitude) = self.getLocation() mapView.camera = GMSCameraPosition.camera( withLatitude: latitude, longitude: longitude, zoom: 14) } } //Get the user location func getLocation() -> (latitude: CLLocationDegrees, longitude: CLLocationDegrees) { let latitude = (locationManager.location?.coordinate.latitude)! let longitude = (locationManager.location?.coordinate.longitude)! return (latitude, longitude) }