I am creating a yelp API app, but I am having trouble with my current location code. It seems the problem is my call to the API is happening before my values of Longitude and latitude are set, but I don't know how to rearrange my code to fix this issue. I have attached my code below lmk if you see anything I can do.
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var venuesTableView: UITableView!
let locationManager = CLLocationManager()
var long: Double = 0.0
var lat: Double = 0.0
/// Central Park, NYC coordinates
let CPLatitude: Double = 40.782483
let CPLongitude: Double = -73.963540
// Menlo Park, California Coordinates
let MPLatitude: Double = 37.4539910200113
let MPLongitude: Double = -122.19097843112596
var venues: [Venue] = []
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations:[CLLocation]) {
guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else{return}
long = locValue.longitude
lat = locValue.latitude
print(long)
print(lat)
}
override func viewDidLoad() {
super.viewDidLoad()
venuesTableView.delegate = self
venuesTableView.dataSource = self
venuesTableView.register(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "customCell")
venuesTableView.separatorStyle = .none
retrieveVenues(latitude: lat, longitude: long, category: "eventservices",
limit: 20, sortBy: "distance", locale: "en_US") { (response, error) in
if let response = response {
self.venues = response
DispatchQueue.main.async {
self.venuesTableView.reloadData()
}
}
print("Does this work?")
print(self.long)
print(self.lat)
}
locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled(){
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
}
func userCurrentLocation(){
}
}
Create a separate function for the api call.
Then call this from the delegate method.