【发布时间】:2019-07-17 10:45:14
【问题描述】:
我想将纬度和经度变量传递给“weatherData”函数(用户的当前位置)。我不能这个操作。我在哪里做错了?
let locationManager = CLLocationManager()
var latitude : Double?
var longitude : Double?
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location: CLLocationCoordinate2D = manager.location?.coordinate else { return }
latitude = location.latitude
longitude = location.longitude
locationManager.stopUpdatingLocation()
}
override func viewDidLoad() {
super.viewDidLoad()
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
weatherData()
}
func weatherData() {
let url = URL(string: "https://api.openweathermap.org/data/2.5/weather?lat=\(latitude)&lon=\(longitude)&units=metric&appid=APIKEY")
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in...
//other codes
...
..
.
}
}
【问题讨论】:
-
您可以打印您的网址并查看您正确传递变量的天气
-
我解决了。感谢您的建议。
标签: swift currentlocation