如何在iOS中使位置更准确?

时间:2015-08-20 20:21:54

标签: ios swift geolocation cllocationmanager

我正在构建基于位置的应用。反正有没有让我通过CLLocationManager获得的位置更准确?

我需要确保精确度在50米以内。

到目前为止,这是我的代码:

func startSignificantChangeUpdates(){
    if (locationManager.respondsToSelector(Selector("requestWhenInUseAuthorization"))) {
        locationManager.requestWhenInUseAuthorization()
    }else {
        locationManager.startUpdatingLocation()
    }

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
    location = locationManager.location
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    if let newLocation = locations.last as? CLLocation {
        //Lets not bother if the location is less than 500 meters
        let oldLocation = location
        location = newLocation
        var distance = oldLocation?.distanceFromLocation(newLocation)
        if distance > 500 {

            NSNotificationCenter.defaultCenter().postNotificationName("location", object: self)
        }
        if distance > 50 {
            NSNotificationCenter.defaultCenter().postNotificationName("imatlocation", object: self)
        }
    }

}

2 个答案:

答案 0 :(得分:1)

您可以更改所需的准确度以使用

kCLLocationAccuracyBest

答案 1 :(得分:1)

为了确保返回的位置,您需要检查位置的horizontalAccuracy,这将为您提供位置的不确定半径(以米为单位),然后您可以决定采取行动或拒绝基于位置它的准确程度。