ios – SWIFT – LocationManager循环多次?

前端之家收集整理的这篇文章主要介绍了ios – SWIFT – LocationManager循环多次?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个locationManager函数获取用户当前位置并发布城市和州的名称.我有一个打印声明,所以我可以在我的控制台检查一切是否正常工作……事实确实如此.但是,它打印城市位置3次.这实际上在我的实际应用程序中引起了一个问题,但这超出了这个问题的范围.

我的功能如下:

  1. var usersLocation: String!
  2.  
  3. var locationManager: CLLocationManager!
  4.  
  5. func locationManager(manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {
  6.  
  7. let userLocation: CLLocation = locations[0]
  8.  
  9.  
  10. CLGeocoder().reverseGeocodeLocation(userLocation) { (placemarks,error) -> Void in
  11.  
  12. if error != nil {
  13.  
  14. print(error)
  15.  
  16. } else {
  17.  
  18. let p = placemarks?.first // ".first" returns the first element in the collection,or nil if its empty
  19. // this code above will equal the first element in the placemarks array
  20.  
  21. let city = p?.locality != nil ? p?.locality : ""
  22. let state = p?.administrativeArea != nil ? p?.administrativeArea : ""
  23.  
  24. self.navigationBar.title = ("\(city!),\(state!)")
  25. self.usersLocation = ("\(city!),\(state!)")
  26. self.locationManager.stopUpdatingLocation()
  27. print(self.usersLocation)
  28. self.refreshPosts()
  29. }
  30. }
  31. }

所以在我的print(self.usersLocation)中,它将在我的控制台中打印三次.这是正常的吗?

更新显示视图

  1. override func viewDidLoad() {
  2. super.viewDidLoad()
  3.  
  4. locationManager = CLLocationManager()
  5. locationManager.delegate = self
  6. locationManager.desiredAccuracy = kCLLocationAccuracyBest
  7. locationManager.requestWhenInUseAuthorization()
  8. locationManager.startUpdatingLocation()
  9.  
  10. self.tableView.rowHeight = UITableViewAutomaticDimension
  11. self.tableView.estimatedRowHeight = 250.0
  12. }

解决方法

我首先建议一些事情:

>在执行reverseGeocodeLocation之前调用stopUpdatingLocation.

您在reverseGeocodeLocation完成处理程序闭包内调用stopUpdatingLocation.问题是这是异步运行的,因此在更新期间,didUpdateLocations可能会收到额外的位置更新.通常,当您第一次启动位置服务时,您将获得许多更新,通常会提高准确性(例如,水平精度值越来越小).如果在启动异步地理编码请求之前关闭位置服务,则可以最大限度地减少此问题.
>您还可以在viewDidLoad中添加distanceFilter,这将最大程度地减少对委托方法的冗余调用

  1. locationManager.distanceFilter = 1000

>您可以使用自己的状态变量来检查反向地理编码过程是否已启动.例如:

  1. private var didPerformGeocode = false
  2.  
  3. func locationManager(manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {
  4. // if we don't have a valid location,exit
  5.  
  6. guard let location = locations.first where location.horizontalAccuracy >= 0 else { return }
  7.  
  8. // or if we have already searched,return
  9.  
  10. guard !didPerformGeocode else { return }
  11.  
  12. // otherwise,update state variable,stop location services and start geocode
  13.  
  14. didPerformGeocode = true
  15. locationManager.stopUpdatingLocation()
  16.  
  17. CLGeocoder().reverseGeocodeLocation(location) { placemarks,error in
  18. let placemark = placemarks?.first
  19.  
  20. // if there's an error or no placemark,then exit
  21.  
  22. guard error == nil && placemark != nil else {
  23. print(error)
  24. return
  25. }
  26.  
  27. let city = placemark?.locality ?? ""
  28. let state = placemark?.administrativeArea ?? ""
  29.  
  30. self.navigationBar.title = ("\(city),\(state)")
  31. self.usersLocation = ("\(city),\(state)")
  32. print(self.usersLocation)
  33. self.refreshPosts()
  34. }
  35. }

猜你在找的iOS相关文章