我有一个 locationManager 函数来获取用户的当前位置并发布城市和州的名称。我有一个打印语句,所以我可以检查我的控制台是否一切正常……确实如此。但是,它打印了 3 次城市位置。这实际上会导致我的实际应用程序出现问题,但这超出了这个问题的重点。
我的函数如下:
var usersLocation: String!
var locationManager: CLLocationManager!
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation: CLLocation = locations[0]
CLGeocoder().reverseGeocodeLocation(userLocation) { (placemarks, error) -> Void in
if error != nil {
print(error)
} else {
let p = placemarks?.first // ".first" returns the first element in the collection, or nil if its empty
// this code above will equal the first element in the placemarks array
let city = p?.locality != nil ? p?.locality : ""
let state = p?.administrativeArea != nil ? p?.administrativeArea : ""
self.navigationBar.title = ("\(city!), \(state!)")
self.usersLocation = ("\(city!), \(state!)")
self.locationManager.stopUpdatingLocation()
print(self.usersLocation)
self.refreshPosts()
}
}
}
所以在我的 print(self.usersLocation) 中,它将在我的控制台中打印三次。这正常吗?
更新以显示 VIEWDIDLOAD
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 250.0
}
最佳答案
我首先建议几件事:
在执行
reverseGeocodeLocation
之前调用stopUpdatingLocation
。您正在
reverseGeocodeLocation
完成处理程序闭包内调用stopUpdatingLocation
。问题在于它是异步运行的,因此didUpdateLocations
可能会在其间接收到额外的位置更新。通常,当您第一次启动位置服务时,您会获得许多更新,而且精度通常会越来越高(例如,horizontalAccuracy
值越来越小)。如果您在启动异步地理编码请求之前关闭位置服务,您将最大限度地减少此问题。您还可以在
viewDidLoad
中添加一个distanceFilter
,这将最大限度地减少对委托(delegate)方法的冗余调用:locationManager.distanceFilter = 1000
您可以使用自己的状态变量来检查反向地理编码过程是否已启动。例如:
private var didPerformGeocode = false func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { // if we don't have a valid location, exit guard let location = locations.first where location.horizontalAccuracy >= 0 else { return } // or if we have already searched, return guard !didPerformGeocode else { return } // otherwise, update state variable, stop location services and start geocode didPerformGeocode = true locationManager.stopUpdatingLocation() CLGeocoder().reverseGeocodeLocation(location) { placemarks, error in let placemark = placemarks?.first // if there's an error or no placemark, then exit guard error == nil && placemark != nil else { print(error) return } let city = placemark?.locality ?? "" let state = placemark?.administrativeArea ?? "" self.navigationBar.title = ("\(city), \(state)") self.usersLocation = ("\(city), \(state)") print(self.usersLocation) self.refreshPosts() } }
关于ios - SWIFT - LocationManager 循环多次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34033506/