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

标签 ios swift google-maps cllocationmanager ios10

CLLocationManager 的委托(delegate)方法

didChangeAuthorizationStatus 和 didUpdateToLocation

没有接到电话。

位置始终使用描述 key 已添加到 info.plist 中,并且当我第一次启动应用程序时我也会收到通知。

我可以看到谷歌地图,但我看不到当前位置,当我更改位置时,它没有更新。基本上委托(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) {

}

查看 Apple 文档 CLLocationManagerDelegate了解更多详情。

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

相关文章:

javascript - Google map v3 - 如何将事件与 Gmap 服务绑定(bind)

ios - 如何将按钮放置在屏幕下半部分的中央

ios - 如何保留 TableView 行?

ios - iOS 多久监控一次 iBeacons?

ios - 将数据附加到帖子正文并上传到 api

ios - 如何以编程方式使用 SF Symbol?

javascript - 我如何使用javascript变量为附近的地方分配类型

javascript - 为谷歌地图 API 中的现有标记添加信息窗口

ios - 最新版本的 Xcode 与 Mountain Lion 不兼容

ios - 在 ios 中使用旋转在圆形 View 中设置图像