CLLocationManager 委托方法未被调用(google 地图已集成)

CLLocationManager delegate methods are not getting called(google maps is integrated)

CLLocationManager的委托方法

didChangeAuthorizationStatus 和 didUpdateToLocation

没有接到电话。

Location Always Usage Description 键已添加到 info.plist 中,我在第一次启动应用程序时也会收到通知。

我可以看到 google 地图,但是我看不到当前位置,当我更改位置时,它没有得到更新。 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)
        }
    }

在您的 info.plist

中添加这两个属性
NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription

根据您的代码,您正在使用 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 文档了解更多详细信息。

检查您的代码后,我发现您需要进行如下更改,

注意:我只添加了位置管理器有问题的代码

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>

您需要将 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)
}