【问题标题】:CLLocationManager delegate methods are not getting called(google maps is integrated)没有调用 CLLocationManager 委托方法(集成了谷歌地图)
【发布时间】:2016-11-21 11:01:11
【问题描述】:

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

【问题讨论】:

  • locationManager.startUpdatingLocation()设置断点是否调用?

标签: ios swift google-maps cllocationmanager ios10


【解决方案1】:

从您的代码中,您正在使用 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 文档了解更多详情。

【讨论】:

    【解决方案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>
    

    【讨论】:

      【解决方案3】:

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

      NSLocationWhenInUseUsageDescription
      NSLocationAlwaysUsageDescription
      

      【讨论】:

      • @narayanaMV 你已经解决了吗?我目前正在处理同样的问题.... :-(
      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 2013-12-03
        • 2011-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-11
        • 2016-09-19
        相关资源
        最近更新 更多