Google Places API SWIFT UI how can i get 60 results at the same call

26 Views Asked by At

I'm working with the Google Places API and aiming to obtain 60 results in one go. Wondering if it's feasible to utilize the nextpagetoken during the initial request to efficiently retrieve the data. Seeking insights or examples on how to maximize the number of results fetched from Google Places API.

class GoogleClient: GoogleClientRequest {
    
    let session = URLSession(configuration: .default)
    var googlePlacesKey: String = "API_KEY"
     
    func getGooglePlacesData(forKeyword keyword: String, location: CLLocation, withinMeters radius: Int, token : String? = nil,  using completionHandler: @escaping (GooglePlacesResponse) -> ())  {
         
        let url = googlePlacesDataURL(forKey: googlePlacesKey, location: location, keyword: keyword, token: token)
         let task = session.dataTask(with: url) { (responseData, _, error) in
                                                
             print("URL is \(url)")
             if let error = error {
                print(error.localizedDescription)
                  return
             }
             if let data = responseData,
                let json = try? JSONSerialization.jsonObject(with: data) as? [String : Any] {
                 print("Results \(json)")
             }
             guard let data = responseData, let response = try? JSONDecoder().decode(GooglePlacesResponse.self, from: data) else {
                 print("empty results")
                 completionHandler(GooglePlacesResponse(results:[], next_page_token: nil))
                  return
             }
             
             if response.next_page_token == nil {
                 completionHandler(response)
             } else {
                 print("Getting next Results")
                 self.getNextResults(keyword: keyword, location: location, token: response.next_page_token, step: 1, results: response.results, completionHandler: completionHandler)
             }
             
           
        }
        task.resume()
    }
    
    func getNextResults(keyword: String, location: CLLocation, token: String? = nil, step: Int, results : [Place], completionHandler: @escaping (GooglePlacesResponse) -> () ) {
        let url = googlePlacesDataURL(forKey: googlePlacesKey, location: location, keyword: keyword, token: token)
         let task = session.dataTask(with: url) { (responseData, _, error) in
                                                
             
             if let error = error {
                print(error.localizedDescription)
                  return
             }
                guard let data = responseData, let response = try? JSONDecoder().decode(GooglePlacesResponse.self, from: data) else {
                 print("empty results")
                 completionHandler(GooglePlacesResponse(results: results, next_page_token: token))
                  return
                }
             
             if step == 2 || response.next_page_token == nil {
                 print("Stop getting next results \(step) \(response.next_page_token)")
                 completionHandler(GooglePlacesResponse(results: results + response.results, next_page_token: token))
             } else {
                 print("Getting next results 2")
                 let totalResults = results + response.results
                 self.getNextResults(keyword: keyword, location: location, token: response.next_page_token, step: step + 1, results: totalResults, completionHandler: completionHandler)
                 
             }
             
             completionHandler(response)
        }
        task.resume()
    }
    
    func googlePlacesDataURL(forKey apiKey: String, location: CLLocation, keyword: String, token : String? = nil) -> URL {
    
         let baseURL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?"
         let locationString = "location=" + String(location.coordinate.latitude) + "," + String(location.coordinate.longitude)
         let rankby = "rankby=distance"
         let keywrd = "keyword=" + keyword
         let key = "key=" + apiKey
//    let url = URL(string: "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Paris&types=geocode&key=" + key)
//        return url!
        if let token = token {
            return URL(string: baseURL + locationString + "&" + rankby + "&" + keywrd + "&" + key + "&pagetoken=\(token)"  )!
        }
         return URL(string: baseURL + locationString + "&" + rankby + "&" + keywrd + "&" + key)!
    }
    
}
0

There are 0 best solutions below