brandonjyuhas
brandonjyuhas

Reputation: 93

Get Image URL From Google Places Photo API

I am attempting to pull a photo URL from a request to the Google Places Api for a Rails application. I have been using the documentation located at: https://developers.google.com/places/documentation/photos#place_photo_response

The issue I'm running in to is that I'm getting an image as the return, not the URL itself. If I navigate by hand to the URL of the API request, I am redirected to a page containing the image. I am not sure how to access the URL that I am redirected to from my application. The code I am using is:

raw_rest = HTTParty.get("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=#{@lat},#{@long}&radius=50000&types=restaurant&key=#{Rails.application.secrets.google_api_key}")


@photo_reference = raw_rest["results"][0]["photos"][0]["photo_reference"]

@photo = HTTParty.get("https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=#{@photo_reference}&key=#{Rails.application.secrets.google_api_key}"

When I run this in the rails console, I can see that the return is an image (as the Google Places API says it will be). I'm not sure how to display the image without the URL, however. If there is a way in Rails to display an image saved to a variable, or a way to pull the redirect URL from HTTParty, that would be great.

Thanks in advance! Let me know if there are any questions!

Upvotes: 2

Views: 910

Answers (0)

Related Questions