【问题标题】:How do I pass latitude and longitude to APIURL?如何将纬度和经度传递给 APIURL?
【发布时间】: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


【解决方案1】:

我会推荐通读this article,以更好地理解函数和语言。

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,以帮助进行一般的整洁和类型强制。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-18
    • 1970-01-01
    • 1970-01-01
    • 2015-01-08
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多