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