gpt4 book ai didi

ios - 位置管理器不会停止更新

转载 作者:搜寻专家 更新时间:2023-11-01 07:16:46 28 4
gpt4 key购买 nike

这是我的locationUpdate委托(delegate)

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {


let lastLocation:CLLocation=locations.last!
let accuracy:CLLocationAccuracy=lastLocation.horizontalAccuracy// the radius of uncertainity location in meters
print("Received location \(lastLocation) with acccuracy \(accuracy)")
if (accuracy < 100.0)
{
let span:MKCoordinateSpan=MKCoordinateSpanMake(0.14,0.14)
let region:MKCoordinateRegion=MKCoordinateRegionMake(lastLocation.coordinate, span)
self.mapview.setRegion(region, animated: true)
manager.stopUpdatingLocation()
print("-------LOCATION UPDATE---------")
self.getLocationData()

}

我以这种方式在 viewDidLoad 中调用它。

override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate=self
self.locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters
self.locationManager.requestAlwaysAuthorization()
self.locationManager.startUpdatingLocation()
}

我的问题是 locationmanager 似乎没有停止更新。我的 getLocationData() 方法触发了 2 次。

最佳答案

if locations.count > 0 {
manager.stopUpdatingLocation()
}

if (accuracy < 100.0)
{
let span:MKCoordinateSpan=MKCoordinateSpanMake(0.14,0.14)
let region:MKCoordinateRegion=MKCoordinateRegionMake(lastLocation.coordinate, span)
self.mapview.setRegion(region, animated: true)
print("-------LOCATION UPDATE---------")
self.getLocationData()

}
else {
manager.startUpdatingLocation()
}

这样你就可以确保 self.getLocationData() 不会被调用,除非准确度小于 100 并且它会被调用一次

关于ios - 位置管理器不会停止更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41779030/

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