谷歌放置Api PlaceDetails照片参考

时间:2012-11-23 07:39:40

标签: api google-places-api

我正在使用Google Place Api的某些结果“photo_reference”(类似于“参考”)值。我找不到有关如何使用它来获取该照片的任何提及。我知道如何使用“引用”来获取PlaceDetail,我确信photo_reference的使用方法类似,但我找不到此photo_reference请求的JSON / XML URL。感谢您的任何帮助。帕维尔

6 个答案:

答案 0 :(得分:34)

请在此处查看文档:{​​{3}}

他们刚刚宣布了这个新的地方照片功能

简而言之,这就是你应该如何使用这个新功能:

https://developers.google.com/places/documentation/photos

只需用你自己的价值代替:

  • PHOTO_REFERENCE
  • MAX_HEIGHT - 1到1600之间的int值
  • MAX_WIDTH - int值从1到1600
  • YOUR_API_KEY

你完成了

答案 1 :(得分:4)

Places API现在支持返回一张地方照片(如果适用于Place Search request)和最多十张地方照片Place Details request

如果您的请求返回了照片数组,则可以将photo_reference从包含的照片对象传递到maxheight和/或maxwidth的{​​{3}}, sensorkey参数:

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

有关详细信息,请参阅Place Photo request

答案 2 :(得分:1)

步骤1:用于调用Google Place Photos的URL是:

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

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

第2步:由于上述URL重定向到另一个URL,请使用HTTPClient,因为它会自动处理重定向内容。

代码:

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()+"");
 }

希望这对所有人都有帮助。

答案 3 :(得分:1)

请记住,没有免费的照片请求了。 目前(2020年11月)为$7.0 for 1000 requests(如果您的交易量不超过100,000)。查看下面的照片。

Google places photo requests billing info

进一步了解Google Places billing info page

答案 4 :(得分:0)

启动地图后,您可以获取带有其图像的地点详细信息

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 }));
        });
    }
  }
);

答案 5 :(得分:0)

解决 Javascript 的 PhotoReference 问题

用户@R.K 在java 中解决了这个问题,但是在js 中你需要使用fetch()。这是我使用的代码:

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);
})

在这里,我使用了 proxyUrl 的 heroku 链接和@Chriss Green 的 url 帖子中显示的网址。希望这可以帮助任何使用 js 感到困惑的人!

相关问题