gpt4 book ai didi

swift - didUpdateLocations 只能调用一次

转载 作者:行者123 更新时间:2023-11-28 05:58:19 28 4
gpt4 key购买 nike

func startLocationManager() {
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
locationManager.startUpdatingLocation()
}
}

获取位置授权后,我在viewDidLoad()中调用了这个方法,然后开始调用func locationManager(_:, didUpdateLocations:)

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let lastLocation = locations.last!

if lastLocation.timestamp.timeIntervalSinceNow < -5 {
return
}
if lastLocation.horizontalAccuracy < 0 {
return
}

var distance = CLLocationDistance(Double.greatestFiniteMagnitude)

if let location = location {
distance = lastLocation.distance(from: location)
}

if location == nil || location!.horizontalAccuracy > lastLocation.horizontalAccuracy {
location = lastLocation
if location.horizontalAccuracy <= locationManager.desiredAccuracy {

stopLocationManager()

getWeather()
}
} else if location == nil || distance < 1 {
let timeInterval = lastLocation.timestamp.timeIntervalSince(location!.timestamp)
if timeInterval > 5 {
stopLocationManager()
getWeather()
}
}
}

这就是我结束 updateLocation 的方式:

func stopLocationManager() {
locationManager.stopUpdatingLocation()
locationManager.delegate = nil
}

终于完美的得到了我想要的位置数据。

但是当我想通过其他方式重新获取新位置时,即已经执行了locationManager.startUpdatingLocation(),仍然无法执行func locationManager(_:, didUpdateLocations:).

我想知道如何解决这个问题。

最佳答案

停止更新位置时不要设置 locationManager.delegate = nil。或者,如果您这样做,请在您想再次开始获取位置更新时将其设置回 self。

关于swift - didUpdateLocations 只能调用一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50681105/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com