ios - CLLocationManager 委托(delegate)方法没有被调用(集成了谷歌地图)

标签 ios swift google-maps cllocationmanager ios10

CLLocationManager的委托(delegate)方法

didChangeAuthorizationStatus 和 didUpdateToLocation

没有被调用。

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

我可以看到谷歌地图,但我看不到当前位置,当我改变位置时,它没有得到更新。 Basicaaly 委托(delegate)方法未被调用。

//代码

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 文档了解更多详情。

关于ios - CLLocationManager 委托(delegate)方法没有被调用(集成了谷歌地图),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40718552/

相关文章:

ios - 如何使用 sharekit API 登录 Facebook?

ios - UIViewController 中的 textLabel.text 在从 NSObject 分配字符串时出现 nil

ios - iOS应用程序中的youtube iFrame在全屏后导致半屏变黑

swift - 从 URL 链接中查找特定字符

ios - 深层链接 URL 方案不会调用 iOS App Delegate 中的任何函数

ios - 值保留在数组内部

ios - 同时从 TableView 行和核心数据中删除项目

google-maps - Google map 形状 - 如何防止重叠

google-maps - ionic 电容器谷歌地图 setMapType 不起作用

javascript - Chrome 更新 38.0.2125.104 m 后,Google Map API v3 不显示多边形