CLLocationManager委托方法没有被调用(谷歌地图已集成) [英] CLLocationManager delegate methods are not getting called(google maps is integrated)

查看:101
本文介绍了CLLocationManager委托方法没有被调用(谷歌地图已集成)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CLLocationManager的委托方法

didChangeAuthorizationStatus

didUpdateToLocation



没有被调用。

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



我可以看到google地图,但我无法看到当前位置,当我更改位置时,它没有更新。

// code

 导入UIKit 
导入GoogleMaps

类ViewController:UIViewController,GMSMapViewDelegate {
@IBOutlet弱var mapViewTest:GMSMapView!
let locationManager = CLLocationManager()
var currentLocation:CLLocation = CLLocation(纬度:0.0,经度:0.0)
var currentLatitude:Double = 0.0
var currentLongitude:Double = 0.0
覆盖func viewDidLoad()
{
super.viewDidLoad()``
locationManager.delegate = self
if(CLLocationManager.locationServicesEnabled())
{
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.allowsBackgroundLocationUpdates = true
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
//做任何额外的加载视图后设置,通常从一个笔尖。




扩展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

$ b 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)在
func locationManager(_ manager:CLLocationManager,didUpdateLocations locations:[CLLocation])之下被弃用{

}

func locationManager(_ manager:CLLocationManager,didFailWithError error:Error){

}

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


The delegate Methods of CLLocationManager

didChangeAuthorizationStatus and didUpdateToLocation

are not getting called.

Location Always Usage Description key is already added in info.plist and I am getting notification also when i launch app for the first time.

I am able to see the google map, but i am not able to see current location, When i change location, its not getting updated. Basicaaly delegate methods are not getting called.

//code

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

解决方案

From your code you are working with Swift 3, and in Swift 3 CLLocationManagerDelegate method's signature is changed like this.

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

}

Check Apple Documentation on CLLocationManagerDelegate for more details.

这篇关于CLLocationManager委托方法没有被调用(谷歌地图已集成)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆