Swift 4 CLLocationManager问题:我可以在ViewDidLoad函数中使用从CLLocationManager获取的location吗?
Swift 4 CLLocationManager question: Can I use the location, which is got from CLLocationManager, in ViewDidLoad function?
按照网上的说明,我能够使用 CLLocationManager 获取我的当前位置。代码结构如下:
var myCurCoordinate:String!
override func viewDidLoad() {
super.viewDidLoad()
getLocation()
print(myCurCoordinate) // HERE I GOT "nil". In my original code, I am not really printing it. Instead, I have another function here that needs to use myCurCoordinate
}
func getLocation() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let loc:CLLocation = locations[0] as CLLocation
myCurCoordinate="\(loc.coordinate.latitude),\(loc.coordinate.longitude)"
print(myCurCoordinate) // HERE print 3 times because of its async
}
任何人都可以帮我弄清楚如何获取位置信息并能够在 viewDidLoad() 中打印它吗?
谢谢!
您无法在 viewDidLoad
中打印您的位置,因为它是一种异步方法,您需要等到它被获取。当您获得您的位置时,您需要停止使用 stopUpdatingLocation()
更新您的位置。为此,我更喜欢使用以下内容。
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {
initialLocation = CLLocation(latitude: location.coordinate.latitude,
longitude: location.coordinate.longitude)
print(initialLocation)
getLocation?(initialLocation)
}
manager.stopUpdatingLocation()
}
如果你坚持要在里面打印它 viewDidLoad()
你可以用闭包来完成。
首先,你需要定义一个闭包。
var getLocation: ((_ location: CLLocation) -> (Void))?
然后在您的 viewDidLoad()
方法中,您需要指定触发闭包时要执行的操作。
getLocation = { location in
print(location)
}
然后你需要使用
在 didUpdateLocations
中触发关闭
getLocation?(initialLocation)
按照网上的说明,我能够使用 CLLocationManager 获取我的当前位置。代码结构如下:
var myCurCoordinate:String!
override func viewDidLoad() {
super.viewDidLoad()
getLocation()
print(myCurCoordinate) // HERE I GOT "nil". In my original code, I am not really printing it. Instead, I have another function here that needs to use myCurCoordinate
}
func getLocation() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let loc:CLLocation = locations[0] as CLLocation
myCurCoordinate="\(loc.coordinate.latitude),\(loc.coordinate.longitude)"
print(myCurCoordinate) // HERE print 3 times because of its async
}
任何人都可以帮我弄清楚如何获取位置信息并能够在 viewDidLoad() 中打印它吗?
谢谢!
您无法在 viewDidLoad
中打印您的位置,因为它是一种异步方法,您需要等到它被获取。当您获得您的位置时,您需要停止使用 stopUpdatingLocation()
更新您的位置。为此,我更喜欢使用以下内容。
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {
initialLocation = CLLocation(latitude: location.coordinate.latitude,
longitude: location.coordinate.longitude)
print(initialLocation)
getLocation?(initialLocation)
}
manager.stopUpdatingLocation()
}
如果你坚持要在里面打印它 viewDidLoad()
你可以用闭包来完成。
首先,你需要定义一个闭包。
var getLocation: ((_ location: CLLocation) -> (Void))?
然后在您的 viewDidLoad()
方法中,您需要指定触发闭包时要执行的操作。
getLocation = { location in
print(location)
}
然后你需要使用
在didUpdateLocations
中触发关闭
getLocation?(initialLocation)