【发布时间】:2020-12-11 15:32:42
【问题描述】:
我在 IOS Swift 中使用 CoreLocation 来获取当前坐标,但在某些情况下,它让我最接近我想要的位置,而有时坐标距离我给定的地址有很多米。请检查我正在使用的代码是否有任何其他方法可以获得准确的当前位置坐标。?
我的代码是:
import CoreLocation
var locationManager:CLLocationManager!
var userLat = 0.0
var userLong = 0.0
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager = CLLocationManager()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled(){
self.locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations:
[CLLocation]) {
guard let userLocation :CLLocation = locations.first else{return}
userLat = userLocation.coordinate.latitude
userLong = userLocation.coordinate.longitude
//current location
print("current location latitude is :%@", userLat)
print("current location longitude is :%@", userLong)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(Error: \(error)")
}
请在当前位置准确但有时距离太远的时候帮助我。谢谢
【问题讨论】:
-
假设您在最后一段中的意思是“两米”,这对于任何消费级 GPS 来说都是相当准确的。在某些情况下,您可以期望获得数百米外的位置。
标签: ios swift core-location cllocation currentlocation