简体   繁体   English

Nodejs)如何使用 Google Place Photo API 获取图像?

[英]Nodejs) How to get image with Google Place Photo API?

I am trying to use Google Place Photos API to retrieve images I managed to get photo references dynamically, but I am not really sure how to retrieve image using this API.我正在尝试使用Google Place Photos API来检索我设法动态获取照片参考的图像,但我不确定如何使用此 API 检索图像。

async function getImageOfPlace(place){
try{
  const response = await fetch('https://maps.googleapis.com/maps/api/place/photo? 
   maxwidth=400&photo_reference='+place+'&key='+apiKey);
   //const json = await response.json();
   console.log(response);
 }catch(e){
   console.log(e);
}
}

Code above shows such response:上面的代码显示了这样的响应:

Response {
 [Symbol(realm)]: null,
 [Symbol(state)]: {
   aborted: false,
   rangeRequested: false,
   timingAllowPassed: true,
   requestIncludesCredentials: false,
   type: 'default',
   status: 200,
   timingInfo: {
     startTime: 2738.1868999004364,
     redirectStartTime: 2738.1868999004364,
     redirectEndTime: 2909.6113998889923,
     postRedirectStartTime: 2909.6113998889923,
     finalServiceWorkerStartTime: 0,
     finalNetworkResponseStartTime: 0,
     finalNetworkRequestStartTime: 0,
     endTime: 0,
     encodedBodySize: 1125,
     decodedBodySize: 773,
     finalConnectionTimingInfo: null
   },
   cacheState: '',
   statusText: 'OK',
   headersList: HeadersList {
     [Symbol(headers map)]: [Map],
     [Symbol(headers map sorted)]: null
   },
   urlList: [ [URL], [URL] ],
   body: { stream: undefined }
 },
 [Symbol(headers)]: HeadersList {
   [Symbol(headers map)]: Map(16) {
     'access-control-expose-headers' => 'Content-Length',
     'content-disposition' => 'inline;filename="2019-12-07.jpg"',
     'vary' => 'Origin',
     'access-control-allow-origin' => '*',
     'timing-allow-origin' => '*',
     'x-content-type-options' => 'nosniff',
     'server' => 'fife',
     'content-length' => '13767',
     'x-xss-protection' => '0',
     'date' => 'Wed, 06 Jul 2022 16:49:15 GMT',
     'expires' => 'Thu, 07 Jul 2022 16:49:15 GMT',
     'cache-control' => 'public, max-age=86400, no-transform',
     'etag' => '"v2041"',
     'content-type' => 'image/jpeg',
     'age' => '428',
     'alt-svc' => 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"'
   },
   [Symbol(headers map sorted)]: null
 }
}

How do I get image or url of the image of the response?如何获取响应图像的图像或 url?

Somebody has told that I can do call directly, putting API url inside image src有人告诉我可以直接调用,将​​ API url 放入 image src

<img src='https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photo_reference='+image+'&key='+apiKey'/>

but I don't think I can do this because storing apiKey in client side, as far as I know it is very very dangerous但我认为我不能这样做,因为据我所知,将 apiKey 存储在客户端非常危险

Any good ideas how to retrieve image using this API?任何好主意如何使用此 API 检索图像?

You'll likely have to get the proper index of the image URL from the response using the [] operator.您可能必须使用 [] 运算符从响应中获取正确的图像 URL 索引。 The docs indicate it could return up to ten images. 文档表明它最多可以返回十张图像。

The response to these requests will contain a photos[] field if the place has related photographic content.如果该地点有相关的照片内容,则对这些请求的响应将包含一个 photos[] 字段。

So something like所以像

return response[1]['urlList'][0]

or wherever the first URL comes back.或第一个 URL 返回的任何地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用IAP和People API获取用户的Google帐户照片? (首选NodeJS) - How can I get a user's Google account photo using IAP and the People API? (NodeJS preferred) 如何使用API​​ nodeJS在React中快速存储照片 - How to store photo fast in React with API nodeJS 处理谷歌照片参考以获取图片网址 - Processing google photo reference to get the image url NodeJS,如何使用谷歌 api 获取带有刷新令牌的新令牌? - NodeJS, How to get new token with refresh token using google api? 从 NodeJS 将图像发送到 Google Drive API - Send image to Google Drive API from NodeJS NodeJS-如何获取图像尺寸? - NodeJS - How to get image dimensions? 如何获取变量中返回的Google Place Autocomplete API结果,而不是将结果通过HTML输入元素提供给客户端 - How to get result of google place Autocomplete API returned in a variable instead of serving result in HTML input element to the client 使用Node.js和请求模块将照片上传到Tumblr API - Upload photo to Tumblr API with nodejs and request module 如何从 Google Sheets API 获取“单元格中的图像”URL - How to get an "Image in cell" URL from the Google Sheets API NodeJS Google Drive API 如何更新文件 - NodeJS Google Drive API how to update file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM