Swift 如何将纬度和经度传递给APIRL?

Swift 如何将纬度和经度传递给APIRL?,swift,currentlocation,Swift,Currentlocation,我想把纬度和经度变量传递给weatherData函数用户的当前位置。我不能做这个手术。我哪里出错了 let locationManager = CLLocationManager() var latitude : Double? var longitude : Double? func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])

我想把纬度和经度变量传递给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
...
..
.
}
}
我建议通读一遍,以便更好地理解函数和语言

func weatherData(locationVariable: CLLocation)  {

let url = URL(string: "https://api.openweathermap.org/data/2.5/weather?lat=\(locationVariable.coordinate.latitude)&lon=\(locationVariable.coordinate.longitude)&units=metric&appid=APIKEY")
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in...

    //other codes
    ...
    ..
    .
}
你要做的是通过你的变量,这样你的最终代码将是

let locationManager = CLLocationManager()
var varLocation = CLLocation()

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let location: CLLocation = manager.location else { return }
    varLocation = location
    locationManager.stopUpdatingLocation()
}
override func viewDidLoad() {
    super.viewDidLoad()
    locationManager.requestAlwaysAuthorization()
    locationManager.requestWhenInUseAuthorization()

    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.startUpdatingLocation()
    }

    weatherData(locationVariable: varLocation)
}

func weatherData(locationVariable: CLLocation)  {
    let url = URL(string: "https://api.openweathermap.org/data/2.5/weather?lat=\(locationVariable.coordinate.latitude)&lon=\(locationVariable.coordinate.longitude)&units=metric&appid=APIKEY")
    let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
        ...
    }
}

我们还将变量更改为CLLocation,以帮助实现总体整洁和强制键入

你可以打印你的url,看看你是否正确地传递了变量。谢谢你的建议。