我正在構建一個API端點,它將使用express.js和Node.js client library for Google Maps API Web Services返回Google地點的詳細信息和單張照片。以下端點會返回某個地點的詳細信息,因此無法檢索照片的網址。以下是有問題的代碼。從GoogleMapsClient響應中獲取Google Place Photo的URL
有兩個請求發送到谷歌地圖的客戶端:
- 獲得基於
req.params.id
地點的詳細信息和細節包含photos
陣列 使用
photo_reference
從上面var googleMapsClient = require('@google/maps').createClient({ key: 'mykeyhere', Promise: Promise }); exports.getGglPlace = function(req, res) { googleMapsClient.place({ placeid: req.params.id }).asPromise() .then((response) => { var venue = response.json.result if (venue.photos[0]) { googleMapsClient.placesPhoto({ photoreference: venue.photos[0].photo_reference, maxwidth: 200 }).asPromise() .then((photo) => { console.log("Photo:", photo); // this returns a massive chunk of data which I think contains the actual image object also venue.photoURL = photo.url; // this doesn't work res.send(venue); }) .catch((err)=>{ console.log("Error Getting Photo!", err); res.send(venue); }) } else { res.send(venue); } }) .catch((err) => { res.send(404); }) }
獲取照片
任何想法如何從響應中獲取URL h在上面的代碼中被稱爲photo
?
如果我嘗試通過瀏覽器或郵遞員直接訪問API,則URL將重定向到圖像的實際源URL,這是我想要添加到我的venue
對象中的內容。
重定向到(這是我想photo.url
回到什麼):https://lh3.googleusercontent.com/p/AF1QipPVD12HA5FBnjmiqVLphYgjfPtIPydn4Ie-xGKr=s1600-w200
任何幫助表示讚賞。
P.S.我在這裏的第一篇文章 - 抱歉,如果我不清楚我的問題。
我發現了一個類似的問題[這裏](HTTPS: //sackoverflow.com/questions/39508823/how-process-response-object-from-google-places-api-placesphoto),但它沒有回答 – boomer4you