Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 未调用CLLocationManager委托方法(集成了google地图)_Ios_Swift_Google Maps_Cllocationmanager_Ios10 - Fatal编程技术网

Ios 未调用CLLocationManager委托方法(集成了google地图)

Ios 未调用CLLocationManager委托方法(集成了google地图),ios,swift,google-maps,cllocationmanager,ios10,Ios,Swift,Google Maps,Cllocationmanager,Ios10,CLLocationManager的委托方法 didChangeAuthorizationStatus 和 DidUpdateLocation 我们没有接到电话 Location Always Usage Description key已添加到info.plist中,并且我在第一次启动应用程序时也会收到通知 我可以看到谷歌地图,但我看不到当前位置,当我改变位置时,它不会得到更新。基本上没有调用委托方法 //代码 import UIKit import GoogleMaps class View

CLLocationManager的委托方法

didChangeAuthorizationStatus 和 DidUpdateLocation

我们没有接到电话

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

我可以看到谷歌地图,但我看不到当前位置,当我改变位置时,它不会得到更新。基本上没有调用委托方法

//代码

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

}

查看上的Apple文档以了解更多详细信息。

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

注意:我只添加了与location manager有问题的代码

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>
n不使用时的位置说明
$(产品名称)位置使用
N位置始终使用说明
$(产品名称)始终使用位置

您需要将mapview委托设置为self

试试这个:

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

locationManager.startUpdatingLocation()中设置断点它被调用了吗?@narayanaMV您已经修复了吗?我目前正在运行相同的问题…:-(