I am trying to display photos of specific locations using a 'photo_reference' provided by Google's Places API.
Currently, I am using Google Places API's 'Place Details' requests to get information about specific locations but am having trouble displaying photos of those locations. Google provides a 'photo_reference' in the 'Place Details' response that is supposed to be used to get specific images but the documentation here isn't very helpful at showing how.
I'm currently making Google Place Details requests like this: (i have a function to create the url, then a function to make the request)
func googlePlacesDetailsURL(forKey apiKey: String, place_ID: String) -> URL {
print("passed place_ID before url creation ", place_ID)
let baseURL = "https://maps.googleapis.com/maps/api/place/details/json?"
let place_idString = "place_id=" + place_ID
let fields = "fields=geometry,name,rating,price_level,types,opening_hours,formatted_address,formatted_phone_number,website,photos"
let key = "key=" + apiKey
print("Details request URL:", URL(string: baseURL + place_idString + "&" + fields + "&" + key)!)
return URL(string: baseURL + place_idString + "&" + fields + "&" + key)!
}
func getGooglePlacesDetailsData(place_id: String, using completionHandler: @escaping (GooglePlacesDetailsResponse) -> ()) {
let url = googlePlacesDetailsURL(forKey: googlePlacesKey, place_ID: place_id)
let task = session.dataTask(with: url) { (responseData, _, error) in
if let error = error {
print(error.localizedDescription)
return
}
guard let data = responseData, let detailsResponse = try? JSONDecoder().decode(GooglePlacesDetailsResponse.self, from: data) else {
print("Could not decode JSON response. responseData was: ", responseData)
return
}
print("GPD response - detailsResponse.result: ", detailsResponse.result)
completionHandler(detailsResponse)
}
task.resume()
}
Does anyone know how to make a Google Place Photo request using a 'photo_reference' in Swift, and display the returned photo?