何时必须调用 [self.locationManager startUpdatingLocation] 以及何时必须停止?
When i have to call [self.locationManager startUpdatingLocation] and when i have to stop?
如果我这样写:
[self.locationManager startUpdatingLocation];
[self.locationManager stopUpdatingLocation];
坐标如何准确?
您可以在 viewWillAppear
中执行 locationManager.startUpdatingLocation()
,然后在 didUpdateLocations 中执行,当您有一个位置时,您可以检查 [horizontalAccuracy][1]
属性,这将
The radius of uncertainty for the location, measured in meters.
(read-only)
您可以使用此行
访问didUpdateLocations
中的horizontalAccuracy
let horizontalAccuracy: CLLocationAccuracy = manager.location!.horizontalAccuracy
如果您对结果满意可以停止更新,否则继续。
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location:CLLocationCoordinate2D = manager.location!.coordinate
lat = String(location.latitude)
long = String(location.longitude)
let horizontalAccuracy: CLLocationAccuracy = manager.location!.horizontalAccuracy
if (horizontalAccuracy < 5.0){
locationManager.stopUpdatingLocation()
}
}
如果发生错误,您可以重试
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
locationManager.requestLocation()
}
如果我这样写:
[self.locationManager startUpdatingLocation];
[self.locationManager stopUpdatingLocation];
坐标如何准确?
您可以在 viewWillAppear
中执行 locationManager.startUpdatingLocation()
,然后在 didUpdateLocations 中执行,当您有一个位置时,您可以检查 [horizontalAccuracy][1]
属性,这将
The radius of uncertainty for the location, measured in meters. (read-only)
您可以使用此行
访问didUpdateLocations
中的horizontalAccuracy
let horizontalAccuracy: CLLocationAccuracy = manager.location!.horizontalAccuracy
如果您对结果满意可以停止更新,否则继续。
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location:CLLocationCoordinate2D = manager.location!.coordinate
lat = String(location.latitude)
long = String(location.longitude)
let horizontalAccuracy: CLLocationAccuracy = manager.location!.horizontalAccuracy
if (horizontalAccuracy < 5.0){
locationManager.stopUpdatingLocation()
}
}
如果发生错误,您可以重试
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
locationManager.requestLocation()
}