boo*_*you 4 javascript google-maps express
我正在构建一个API终结点,该终结点将使用express.js和Node.js客户端库(用于Google Maps API Web服务)返回详细信息和一张Google地方的照片。下面的端点返回一个地点的详细信息,我在检索照片的URL时遇到了麻烦。下面是有问题的代码。
对Google Maps客户端有两个请求:
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);
})
}
Run Code Online (Sandbox Code Playgroud)任何想法如何从photo上面的代码中调用的响应中获取URL ?
如果我尝试直接通过浏览器或Postman转到API,则URL将重定向到图像的实际源URL,这就是我要添加到venue对象中的内容。
重定向到(这是我要photo.url返回的内容):https : //lh3.googleusercontent.com/p/AF1QipPVD12HA5FBnjmiqVLphYgjfPtIPydn4Ie-xGKr=s1600-w200
任何帮助表示赞赏。
PS我的第一篇文章-很抱歉,如果我对我的问题不够清楚。
这是我解决的方法。它似乎并不完美,但是可以完成任务。
响应包含一个req部分,其中包含一些键,我可以使用这些键来构建URL。具体来说,就是response.req.socket._host钥匙和response.req.path钥匙。
这是我的代码中的样子(photo来自Google API的响应)
venue.photoURL = "https://" + photo.req.socket._host + photo.req.path;
photo.req.socket._host给我lh3.googleusercontent.com
photo.req.path给我/ p / AF1QipPVD12HA5FBnjmiqVLphYgjfPtIPydn4Ie-xGKr = s1600-w200
照片的构造URL的结果是: https://lh3.googleusercontent.com/p/AF1QipPVD12HA5FBnjmiqVLphYgjfPtIPydn4Ie-xGKr=s1600-w200
| 归档时间: |
|
| 查看次数: |
2275 次 |
| 最近记录: |