尝试将 Lat 和 Long 坐标分配给变量,返回 nil
Trying to assign Lat and Long coordinates to variables, comes back nil
我正在尝试从获取当前位置的函数中分配值。打印语句打印纬度和经度坐标,但变量返回 nil。
我试过将其移动到视图中,但结果仍然相同。
override func viewDidLoad() {
super.viewDidLoad()
// Ask for Authorisation from the User.
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
currentLat = locValue.latitude
currentLong = locValue.longitude
print("locations = \(locValue.latitude) \(locValue.longitude)")
}
//getWeather()
}
我将此功能移动到应用程序中的另一个视图控制器,因为它没有在我的原始视图加载时及时设置坐标。
我还用过:
locations.last?.coordinate.longitude
相对于:
guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
currentLong = locValue.longitude
我确定有更好的方法来做到这一点,但这很有效。
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
currentLong = locations.last?.coordinate.longitude
currentLat = locations.last?.coordinate.latitude
print("locations = \(currentLong!) \(currentLat!)")
}
我正在尝试从获取当前位置的函数中分配值。打印语句打印纬度和经度坐标,但变量返回 nil。
我试过将其移动到视图中,但结果仍然相同。
override func viewDidLoad() {
super.viewDidLoad()
// Ask for Authorisation from the User.
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
currentLat = locValue.latitude
currentLong = locValue.longitude
print("locations = \(locValue.latitude) \(locValue.longitude)")
}
//getWeather()
}
我将此功能移动到应用程序中的另一个视图控制器,因为它没有在我的原始视图加载时及时设置坐标。
我还用过:
locations.last?.coordinate.longitude
相对于:
guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
currentLong = locValue.longitude
我确定有更好的方法来做到这一点,但这很有效。
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
currentLong = locations.last?.coordinate.longitude
currentLat = locations.last?.coordinate.latitude
print("locations = \(currentLong!) \(currentLat!)")
}