当我必须调用[self.locationManager startUpdatingLocation]并且我必须停止时?

时间:2016-01-04 17:33:33

标签: ios core-location cllocationmanager

如果我写这样的话:

[self.locationManager startUpdatingLocation];
[self.locationManager stopUpdatingLocation];

准确是怎样的坐标?

1 个答案:

答案 0 :(得分:0)

您可以在locationManager.startUpdatingLocation()中执行viewWillAppear,然后在didUpdateLocations中,当您有位置时,可以检查[horizontalAccuracy][1]属性

  

位置的不确定半径,以米为单位。   (只读)

您可以使用此行访问horizontalAccuracy中的didUpdateLocations

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()
}