网址查询项不抓取位置坐标



我想知道我的问题是什么原因。我正在使用核心位置来获取我的坐标位置,我在网络方法中将其用作 URLQueryItem 以便从 API 获取响应。但是控制台输出显示纬度查询和经度查询都等于 0,而我有我的纬度和经度值。我在视图加载中使用网络方法。

感谢您的所有回复和解释。

  var queryLattitudeItem : Double = 0
  var queryLongitudeItem : Double = 0
func network () {
        let configuration = URLSessionConfiguration.default
        configuration.waitsForConnectivity = true
        let session = URLSession(configuration: configuration)
        guard let urls = URL(string:"https://api.yelp.com/v3/businesses/search") else { return }
        var urlcomponent = URLComponents(string: "(urls)")
        let queryLat = URLQueryItem(name:"latitude" , value: "(queryLattitudeItem)")
        let queryLong = URLQueryItem(name: "longitude", value: "(queryLongitudeItem)")
        let queryItemterm = URLQueryItem(name: "term", value: "restaurant")
        let queryLimit = URLQueryItem(name: "limit", value: "10")
        urlcomponent?.queryItems = [queryItemterm,queryLat,queryLong,queryLimit]
        print(urlcomponent!)
        print(queryLat)
        print(queryLong)
        var request = URLRequest(url: urlcomponent!.url!)
        request.httpMethod = "GET"
        request.addValue("Bearer (apiKey)", forHTTPHeaderField: "Authorization")
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        let task = session.dataTask(with: request) { (data, response, error) in
            if let response = response as? HTTPURLResponse {
                print(response)
            } else{
                print("error")
            }
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location = locations[locations.count - 1]
        if location.horizontalAccuracy > 0 {
            locationManager.stopUpdatingLocation()
          print("(location.coordinate.longitude), (location.coordinate.latitude)")
        }
        let latitude : Double = (location.coordinate.latitude)
        let longitude : Double = location.coordinate.longitude
        print("This is lat: (latitude), et long(longitude)")
        queryLattitudeItem = latitude
        queryLongitudeItem = longitude

    }

控制台输出

https://api.yelp.com/v3/businesses/search?term=restaurant&latitude=0.0&longitude=0.0&limit=10
latitude=0.0
longitude=0.0
-73.984638, 40.759211
This is lat: 40.759211, et long-73.984638
<NSHTTPURLResponse: 0x600003a91ec0> { URL: https://api.yelp.com/v3/businesses/search?term=restaurant&latitude=0.0&longitude=0.0&limit=10 } { Status Code: 200, Headers {
    "Accept-Ranges" =     (

我会对你的代码做的一件风格上的事情是利用某种结构来存储字符串,这样它们就不会在你的代码中乱扔。当出现问题时,你可以去一个地方调试它,而不是翻阅一堆代码。在这里,我将字符串作为静态 let 存储在枚举中(b/c 我讨厌 rawValues(:

enum Endpoint {
    static let yelp = "https://api.yelp.com/v3/businesses/search"
}

接下来,我将放弃纬度和经度的 var 声明:

var queryLattitudeItem : Double = 0 // 🚫 nuke
var queryLongitudeItem : Double = 0 // 🚫 nuke

相反,我会更新您的网络请求方法以直接从委托方法接受CLLocationCoordinate2D,如下所示:

func getYelpInfo(for coordinate: CLLocationCoordinate2D) {
    // omitted your networking code...this is just the URL creation code
    var components = URLComponents(string: Endpoint.yelp)
    let queryLat = URLQueryItem(name: "latitude", value: String(coordinate.latitude))
    let queryLong = URLQueryItem(name: "longitude", value: String(coordinate.latitude))
    let queryLimit = URLQueryItem(name: "limit", value: "10")
    components?.queryItems = [queryLat, queryLong, queryLimit]
    // You could use a guard statement here if you want to exit out, too
    if let url = components?.url {
        var request = URLRequest(url: url)
        // do your networking request
    }

    print(components!.url!.absoluteString)
}

接下来,在您的didUpdateLocations中,我将调用更新的方法,如下所示:

getYelpInfo(for: location.coordinate)

更新后的方法如下所示:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations[locations.count - 1]
    if location.horizontalAccuracy > 0 {
        locationManager.stopUpdatingLocation()
        getYelpInfo(for: location.coordinate)
        print("(location.coordinate.longitude), (location.coordinate.latitude)")
    }
}

最新更新