CLLocationManager委托方法未被调用(集成了谷歌地图)

时间:2016-11-21 11:01:12

标签: ios swift google-maps cllocationmanager ios10

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)
        }
    }

4 个答案:

答案 0 :(得分:3)

从你的代码中使用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文档了解详情。

答案 1 :(得分:2)

检查完代码后,我发现了一些您需要做的更改,如下所示

注意:我只添加了位置管理器

的代码
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>

答案 2 :(得分:0)

在info.plist中添加这两个属性

NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription

答案 3 :(得分:0)

你需要把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)
}