Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google place Api PlaceDetails Photo Reference

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

like image 532
Pavel Avatar asked Nov 23 '12 07:11

Pavel


4 Answers

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

like image 51
Pavel Kostenko Avatar answered Oct 09 '22 07:10

Pavel Kostenko


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.

like image 42
Chris Green Avatar answered Oct 09 '22 06:10

Chris Green


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.

like image 3
Aleksandar Avatar answered Oct 09 '22 06:10

Aleksandar


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.

like image 2
R.K Avatar answered Oct 09 '22 07:10

R.K