不能调用 locationManager(_::didUpdateLocations:)
Can not called locationManager(_::didUpdateLocations:)
我尝试在用户启动应用程序时获取坐标。
我在一个独立的文件中设置了 locationManager 代码:
UserLocation.swift:
import Foundation
import CoreLocation
class UserLocation: NSObject, CLLocationManagerDelegate {
var userCurrentLocation: CLLocationCoordinate2D?
let locationManager = CLLocationManager()
func locationSetup() {
locationManager.requestWhenInUseAuthorization()
if CLLocationManager.authorizationStatus() != CLAuthorizationStatus.authorizedWhenInUse {
print("authorization error")
return
}
locationManager.distanceFilter = 300
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.delegate = self
locationManager.startUpdatingLocation()
print("startUpdatingLocation")
if CLLocationManager.locationServicesEnabled() {
print("locationServicesEnabled")
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("locationManager getting start")
if let location = locations.last {
self.userCurrentLocation = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
print("print location in locationManager: \(String(describing: userCurrentLocation?.latitude))")
locationManager.stopUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didFinishDeferredUpdatesWithError error: Error?) {
print(error?.localizedDescription as Any)
return
}
}
然后,我这样调用 AppDelegate.swift 的 application(_::didFinishLaunchingWithOptions:)
中的函数:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let getUserLocation = UserLocation()
getUserLocation.locationSetup()
}
但是,只成功调用了locationSetup()
函数,没有调用相关函数locationManager(_::didUpdateLocations:)。 print("locationManager getting start")
我把第一行放在 locationManager(_::didUpdateLocations:)
里,从来没有打印出来
顺便说一句,在 info.plist 中,我已经设置了 Privacy - Location When In Use Usage Description 。
谁能帮帮我?
你的getUserLocation
是局部变量。它会在您创建后 1 毫秒后消失。所以它从来没有时间 做 任何事情(例如位置更新)。将其提升为实例变量,使其寿命更长:
var getUserLocation : UserLocation?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.getUserLocation = UserLocation()
self.getUserLocation?.locationSetup()
return true
}
(另外请使用更好的变量名。对象引用不应以动词开头。)
请 cross-check 模拟器位置:Simulator -> Debug -> Locations 在你的情况下不应该 None...
希望这对您有所帮助...谢谢 :)
我尝试在用户启动应用程序时获取坐标。
我在一个独立的文件中设置了 locationManager 代码:
UserLocation.swift:
import Foundation
import CoreLocation
class UserLocation: NSObject, CLLocationManagerDelegate {
var userCurrentLocation: CLLocationCoordinate2D?
let locationManager = CLLocationManager()
func locationSetup() {
locationManager.requestWhenInUseAuthorization()
if CLLocationManager.authorizationStatus() != CLAuthorizationStatus.authorizedWhenInUse {
print("authorization error")
return
}
locationManager.distanceFilter = 300
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.delegate = self
locationManager.startUpdatingLocation()
print("startUpdatingLocation")
if CLLocationManager.locationServicesEnabled() {
print("locationServicesEnabled")
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("locationManager getting start")
if let location = locations.last {
self.userCurrentLocation = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
print("print location in locationManager: \(String(describing: userCurrentLocation?.latitude))")
locationManager.stopUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didFinishDeferredUpdatesWithError error: Error?) {
print(error?.localizedDescription as Any)
return
}
}
然后,我这样调用 AppDelegate.swift 的 application(_::didFinishLaunchingWithOptions:)
中的函数:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let getUserLocation = UserLocation()
getUserLocation.locationSetup()
}
但是,只成功调用了locationSetup()
函数,没有调用相关函数locationManager(_::didUpdateLocations:)。 print("locationManager getting start")
我把第一行放在 locationManager(_::didUpdateLocations:)
里,从来没有打印出来
顺便说一句,在 info.plist 中,我已经设置了 Privacy - Location When In Use Usage Description 。
谁能帮帮我?
你的getUserLocation
是局部变量。它会在您创建后 1 毫秒后消失。所以它从来没有时间 做 任何事情(例如位置更新)。将其提升为实例变量,使其寿命更长:
var getUserLocation : UserLocation?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.getUserLocation = UserLocation()
self.getUserLocation?.locationSetup()
return true
}
(另外请使用更好的变量名。对象引用不应以动词开头。)
请 cross-check 模拟器位置:Simulator -> Debug -> Locations 在你的情况下不应该 None...
希望这对您有所帮助...谢谢 :)