为什么位置管理器在 viewDidLoad 中为 nil 并且没有调用 didUpdateLocations func 我使用的是真实设备?
Why the location Manager is nil in the viewDidLoad and didUpdateLocations func isnot called I am using a real device?
我正在使用真实设备获取我的当前位置问题是 locationManager.location
是 nil
并且未调用函数 didUpdateLocations
。
var location = CLLocationManager()
@IBOutlet weak var map: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
map.showsPointsOfInterest = true
map.showsScale = true
map.showsUserLocation = true
locationManagerConfiguration()
}
func locationManagerConfiguration(){
location.requestAlwaysAuthorization()
location.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled(){
location.delegate = self
location.desiredAccuracy = kCLLocationAccuracyBest
location.startUpdatingLocation()
}
let sourceCoordinates = location.location?.coordinate
let sourcePlacemark = MKPlacemark(coordinate: sourceCoordinates!
这里是源坐标为零的问题
您提供的代码与您问题中的陈述不符,但我想我在代码中看到了问题。根据苹果文档:Startupdatinglocation
location.startUpdatingLocation()
是一个异步函数,需要几秒钟来获取位置。该函数将立即 return,但当系统获得 GPS 位置时(几秒后,异步)将调用委托回调。您上面的代码正在调用 startUpdatingLocation()
并立即期望出现一个值(同步,它永远不会出现)。您的代码需要更像:
func locationManagerConfiguration(){
location.requestAlwaysAuthorization()
location.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled(){
location.delegate = self
location.desiredAccuracy = kCLLocationAccuracyBest
location.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// TODO: Check the array is not empty
let sourceCoordinates = locations[0]
let sourcePlacemark = MKPlacemark(coordinate: sourceCoordinates!)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Location error: \(error)")
}
我正在使用真实设备获取我的当前位置问题是 locationManager.location
是 nil
并且未调用函数 didUpdateLocations
。
var location = CLLocationManager()
@IBOutlet weak var map: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
map.showsPointsOfInterest = true
map.showsScale = true
map.showsUserLocation = true
locationManagerConfiguration()
}
func locationManagerConfiguration(){
location.requestAlwaysAuthorization()
location.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled(){
location.delegate = self
location.desiredAccuracy = kCLLocationAccuracyBest
location.startUpdatingLocation()
}
let sourceCoordinates = location.location?.coordinate
let sourcePlacemark = MKPlacemark(coordinate: sourceCoordinates!
这里是源坐标为零的问题
您提供的代码与您问题中的陈述不符,但我想我在代码中看到了问题。根据苹果文档:Startupdatinglocation
location.startUpdatingLocation()
是一个异步函数,需要几秒钟来获取位置。该函数将立即 return,但当系统获得 GPS 位置时(几秒后,异步)将调用委托回调。您上面的代码正在调用 startUpdatingLocation()
并立即期望出现一个值(同步,它永远不会出现)。您的代码需要更像:
func locationManagerConfiguration(){
location.requestAlwaysAuthorization()
location.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled(){
location.delegate = self
location.desiredAccuracy = kCLLocationAccuracyBest
location.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// TODO: Check the array is not empty
let sourceCoordinates = locations[0]
let sourcePlacemark = MKPlacemark(coordinate: sourceCoordinates!)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Location error: \(error)")
}