21

I am using Google Place Api where is on some results "photo_reference" (similar to "reference") value. I cannot find any mention about that how to use it to get that photo. I know how to use "reference" to get PlaceDetail and I am sure that usage of photo_reference will be similar, but I cannot find JSON/XML URL for this photo_reference request. Thank you for any help. Pavel

Pavel
  • 361
  • 1
  • 2
  • 9

6 Answers6

40

Please take a look at documentation here: https://developers.google.com/places/documentation/photos

They've just announced this new Place Photos feature

In short this is how you should use this new feature:

https://maps.googleapis.com/maps/api/place/photo?photoreference=PHOTO_REFERENCE&sensor=false&maxheight=MAX_HEIGHT&maxwidth=MAX_WIDTH&key=YOUR_API_KEY

just substitute your own values in place of:

  • PHOTO_REFERENCE
  • MAX_HEIGHT - int value from 1 to 1600
  • MAX_WIDTH - int value from 1 to 1600
  • YOUR_API_KEY

and you are done

Pavel Kostenko
  • 1,859
  • 1
  • 18
  • 18
  • Is there any limit for requesting the images/day with the key? – Harish Kumar Sep 05 '16 at 07:41
  • @HarishKumar 1,000 free requests per 24 hour period. You can increase this limit free of charge, up to 150,000 requests per 24 hour period, by enabling billing on the Google API Console – Pavel Kostenko Sep 06 '16 at 09:16
  • 2
    The PHOTO_REFERENCE is not present in response now. – Biranchi Jul 18 '17 at 16:42
  • @Biranchi check this API: https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&type=restaurant&keyword=cruise&key=YOUR_API_KEY , add your api key here. You'll see photo_reference – Pavel Kostenko Jul 19 '17 at 13:44
  • I am using the nearbySearch in google.maps.places.PlacesService , the photo_reference is not present. i am only getting height, width and html_attributions. – Biranchi Jul 19 '17 at 14:22
  • I guess you need to post some code samples with your params, or something like this. Maybe you're using different API – Pavel Kostenko Jul 19 '17 at 16:18
  • i don't see the photo_reference either anymore. I've made a post here: https://stackoverflow.com/questions/45750833/where-do-i-retrieve-the-photo-reference-value-to-make-a-google-places-photos-req – jchi2241 Aug 18 '17 at 07:38
  • 3
    Hi I successfully made the API request now I see a photo. How do I get the url of that photo that is displayed in JS? – most venerable sir Aug 19 '17 at 18:47
  • The `sensor` parameter is no more useful – Baudev Mar 13 '18 at 18:51
  • @PavelKostenko... I get HTTP 302 error while accessing this URL form REST API or Android code... Any clue what can I do? – R.K Jul 19 '18 at 15:59
  • How can I get the profile image of google business? – Bhavin Thummar Sep 17 '19 at 10:46
  • @PavelKostenko please bear in mind that there is no free photo requests anymore. Read [this page on Google's billing info](https://developers.google.com/places/web-service/usage-and-billing#places-photo) – Aleksandar Nov 13 '20 at 11:00
  • Kindly tell me how to get photo_reference parameter value. – Ahsan Khan Dec 01 '20 at 08:06
5

The Places API now supports the return of one place photo if available for a Place Search request and up to ten place photos for a Place Details request.

If a photos array is returned with your request, you can pass the photo_reference from a contained photo object to a Place Photo request with the maxheight and/or maxwidth, sensor and key parameters:

https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=CnRvAAAAwMpdHeWlXl-lH0vp7lez4znKPIWSWvgvZFISdKx45AwJVP1Qp37YOrH7sqHMJ8C-vBDC546decipPHchJhHZL94RcTUfPa1jWzo-rSHaTlbNtjh-N68RkcToUCuY9v2HNpo5mziqkir37WU8FJEqVBIQ4k938TI3e7bf8xq-uwDZcxoUbO_ZJzPxremiQurAYzCTwRhE_V0&sensor=false&key=AddYourOwnKeyHere

Please see the documentation for more details.

Chris Green
  • 4,397
  • 1
  • 23
  • 21
  • 5
    i see the photo when I type the request url into my browser search bar. The url then becomes the url of the photo. How do I get this url? I only see a photo but I cannot use it in JS in any way. – most venerable sir Aug 19 '17 at 18:54
  • Suppose I am using a photo in my application using this API, should I credit the user who took this photo or is this optional? – Abhinav Mar 21 '18 at 03:17
  • @mostvenerablesir... I found the solution to this problem. Given detailed answer below – R.K Jul 19 '18 at 18:12
  • @mostvenerablesir see my solution to the issue above for js – Darrow Hartman Apr 05 '21 at 04:03
3

please bear in mind that there are no free photo requests anymore. At this moment (November 2020), it is $7.0 for 1000 requests (if your volume is up to 100,000). Check the photo below.

Google places photo requests billing info

Read more on Google Places billing info page.

Aleksandar
  • 3,558
  • 1
  • 39
  • 42
2

Step 1: The URL you should use to call Google Place Photos is :

String url = https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=PHOTOREF&key=YOUR_API_KEY

Refer: https://developers.google.com/places/web-service/photos

Step 2: Since the above URL redirects to another URL, use HTTPClient, as it automatically handles redirect stuff.

Code:

DefaultHttpClient hc = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpContext context = new BasicHttpContext();
hc.setRedirectHandler(new DefaultRedirectHandler() {
 @Override
 public URI getLocationURI(HttpResponse response,
 HttpContext context) throws org.apache.http.ProtocolException {

  //Capture the Location header here - This is your redirected URL
  System.out.println(Arrays.toString(response.getHeaders("Location")));

  return super.getLocationURI(response,context);

  }
 });

 // Response contains the image you want. If you test the redirect URL in a browser or REST CLIENT you can see it's data
 HttpResponse response = hc.execute(httpget, context);
 if(response.getStatusLine().getStatusCode() == 200) {
   // Todo: use the Image response 
   HttpEntity entity = response.getEntity();
   if (entity != null) {
     InputStream instream = entity.getContent();
     Bitmap bmp = BitmapFactory.decodeStream(instream);         
     ImageView imageView = new ImageView(context);
     imageView.setImageBitmap(bmp);
     images.add(imageView);
     instream.close();
  }                 
 }
 else {
   System.out.println(response.getStatusLine().getStatusCode()+"");
 }

Hope this helps everyone.

R.K
  • 1,721
  • 17
  • 22
  • 1
    is this possible doing in javascript instead of java? I cant figure out how to get the url from within the clients browser with JS. Im using the cors-anywhere.herokuapp.com proxy service as well (https://github.com/Rob--W/cors-anywhere) – dcts Jul 10 '19 at 12:04
  • 1
    @thomas here is a detailed article about using the google photos in JavaScript - https://codeburst.io/adding-city-images-to-your-react-app-14c937df2db2 – Aleksandar Nov 13 '20 at 10:57
  • @thomas see my post above for a js version – Darrow Hartman Apr 05 '21 at 04:03
2

After initiating map you can get place details with it's images

const service = new window.google.maps.places.PlacesService(map);
service.getDetails(
  {
    placeId: "some_place_id_here"
  },
  (data, status) => {
    if (status === window.google.maps.places.PlacesServiceStatus.OK) {
      data.photos &&
        data.photos.forEach(photo => {
          console.log(photo.getUrl({ maxWidth: 500, maxHeight: 500 }));
        });
    }
  }
);
Mas
  • 1,267
  • 1
  • 14
  • 13
0

Solving the PhotoReference issue for Javascript

User @R.K solved this issue in java, however in js you need to use fetch(). Here's the code I used:

await fetch(proxyUrl+url).then(async(ref)=>{

await ref.blob()}).then((image)=>{

// do what you need to do
console.log(image)

}).catch((err)=>{
  console.log(err);
})

In this, I used a heroku link for the proxyUrl and the url shown in @Chriss Green's post for url. Hope this helps anyone confused using js!

Darrow Hartman
  • 4,142
  • 2
  • 17
  • 36