1

I am wondering what can the be reason of my issue. I am using core location in order to get the my coordinates location, which I use in the network method as a URLQueryItem in order to get a response from the API. But the console output shows that the latitude query and longitude query are both equal to 0 while I have my a latitude and longitude value. I use the network method inside my viewdidload.

Thanks for all responses and explanations.

  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


    }

Console output

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" =     (
4

1 回答 1

0

我会对你的代码做的一件风格的事情是利用某种结构来存储字符串,这样它们就不会在你的代码中乱扔垃圾。当出现问题时,您可以去一个地方进行调试,而不是翻阅一堆代码。在这里,我将字符串作为静态 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)")

    }
}
于 2019-07-23T04:15:01.987 回答