未调用位置管理器 didUpdateLocations
Posted
技术标签:
【中文标题】未调用位置管理器 didUpdateLocations【英文标题】:Location manager didUpdateLocations not being called 【发布时间】:2017-04-19 01:52:04 【问题描述】:由于某种原因,didUpdateLocations
没有被调用,即使我将委托设置为视图控制器也是如此。在info.plist
中,我将键Privacy - Location When In Use Usage Description
设置为描述。所以我不确定我做错了什么?
import MapKit
import CoreLocation
class OptionsViewController: UIViewController, UITableViewDelegate, CLLocationManagerDelegate
override func viewDidLoad()
//Ask user for location
locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()
//Use users current location if no starting point set
if CLLocationManager.locationServicesEnabled()
let locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
override func viewDidAppear(_ animated: Bool)
locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
locationManager.stopUpdatingLocation()
print(error)
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
print("Did update location called?")
let locValue:CLLocationCoordinate2D = manager.location!.coordinate
print("locations = \(locValue.latitude) \(locValue.longitude)")
【问题讨论】:
问题出在你声明 locationManager 的位置。您需要将其声明为视图控制器的属性 顺便说一句,这不是您的实际代码。 locationManager 在哪里声明?您的代码无法按原样编译 你的问题是let locationManager = CLLocationManager()
在方法内部创建了一个新的局部变量 locationManager,一旦 viewDidLoad 方法完成,它就会不存在
如果您已经在导入 MapKit,则无需导入 CoreLocation
【参考方案1】:
问题就在这里:
override func viewDidAppear(_ animated: Bool)
locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()
此方法将在viewDidLoad
之后调用,但您稍后在viewDidAppear
中新创建了一个 locationManager,并且没有将其委托指向自身。这就是为什么不调用委托(自我)的方法的原因。
改进但不是最好的方法是:
class OptionsViewController: UIViewController, UITableViewDelegate, CLLocationManagerDelegate
let locationManager = CLLocationManager()
override func viewDidLoad()
//Ask user for location
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
//Use users current location if no starting point set
if CLLocationManager.locationServicesEnabled()
if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse
|| CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways
locationManager.startUpdatingLocation()
else
locationManager.requestWhenInUseAuthorization()
else
//Alert user to open location service, bra bra bra here...
override func viewDidAppear(_ animated: Bool)
super.viewDidAppear(animated)
//... nothing need to do for location here
public func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus)
if status == CLAuthorizationStatus.authorizedWhenInUse
|| status == CLAuthorizationStatus.authorizedAlways
locationManager.startUpdatingLocation()
else
//other procedures when location service is not permitted.
//......
【讨论】:
locationManager = CLLocationManager()
?????不是var
或let
。只需抛出代码,看看会发生什么
现在是可选的,您需要更改其余代码
由于上次更新,您的更新现在有一些错误
我又更新了,现在应该没有错误了,谢谢建议,;)
我已经复制并粘贴了您的代码,但 didUpdateLocations
永远不会被调用...以上是关于未调用位置管理器 didUpdateLocations的主要内容,如果未能解决你的问题,请参考以下文章
即使我调用 stopUpdatingLocation 仍然显示位置管理器图标
当我调用位置管理器时无法解析方法“requestLocationUpdates”
为啥 iPhone GPS 箭头出现在状态栏中,即使我没有在位置管理器上调用 startUpdatingLocation?