如何在 iOS 中使位置更准确?
How to make location more accurate in iOS?
我正在构建一个基于位置的应用程序。有没有办法让我通过 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)
}
}
}
您可以更改所需的准确度以使用
kCLLocationAccuracyBest
为了返回位置的准确性,您需要检查位置的 horizontalAccuracy
,这将为您提供位置的不确定半径(以米为单位),然后您可以决定采取行动还是拒绝位置取决于它的准确程度。
我正在构建一个基于位置的应用程序。有没有办法让我通过 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)
}
}
}
您可以更改所需的准确度以使用
kCLLocationAccuracyBest
为了返回位置的准确性,您需要检查位置的 horizontalAccuracy
,这将为您提供位置的不确定半径(以米为单位),然后您可以决定采取行动还是拒绝位置取决于它的准确程度。