I'm building an API endpoint which will return details and a single photo of a Google place using express.js and Node.js client library for Google Maps API Web Services. The endpoint below returns details of a place and I'm having trouble retrieving the URL of a photo. Below is the code in question.
There are two requests to the google maps client:
req.params.id
and the details contain a photos
arrayGet photo using the photo_reference
from above
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);
})
}
Any idea how to obtain the URL from the response which is called photo
in the code above?
If I try going to the API directly through the browser or Postman, the URL gets redirected to the actual source URL of the image, which is what I want to add to my venue
object.
API request example: https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=CmRaAAAA8NPP1nJJ7RCzcQDGUrpBHJXlzlQkN74dcyQJ2ytVpeYIu47sR-8dfCjke1J5exP-HpkayaXOc26ShsVKkXOaJZBOdpmExUfCzUTIBN3x0uPfR5Nt3PnN-a3GoRVZ7YxKEhBvqXF356Tn9mBJ7lA_JQ_7GhQMKvZkOk-Rs9knsansx5yuhfIvsQ&sensor=false&key=mykeyhere
Redirects to (this is what I want photo.url
to return): https://lh3.googleusercontent.com/p/AF1QipPVD12HA5FBnjmiqVLphYgjfPtIPydn4Ie-xGKr=s1600-w200
Any help is appreciated.
P.S. My first post here - sorry if I'm not clear enough with my question.
photo_reference. A string identifier that uniquely identifies a photo. Photo references are returned from either a Place Search or Place Details request.
Getting startedGo to the Google Cloud Console. Click the Select a project button, then select the same project you set up for the Maps JavaScript API and click Open. From the list of APIs on the Dashboard, look for Places API. If you see the API in the list, you're all set.
Customer can temporarily cache latitude (lat) and longitude (lng) values from the Places API for up to 30 consecutive calendar days, after which Customer must delete the cached latitude and longitude values. Customer can cache Places API Place ID (place_id) values, in accordance with the Places API Policies.
Here is how I solved it. It doesn't seem perfect, but it does the job.
The response contains a req section which has keys in there that I was able to use to build the URL. Specifically, the response.req.socket._host
key and the response.req.path
key.
Here is what it looks like in my code (where photo
is the response from google API)
venue.photoURL = "https://" + photo.req.socket._host + photo.req.path;
photo.req.socket._host
gives me lh3.googleusercontent.com
photo.req.path
gives me /p/AF1QipPVD12HA5FBnjmiqVLphYgjfPtIPydn4Ie-xGKr=s1600-w200
Results of the constructed URL to the photo is: https://lh3.googleusercontent.com/p/AF1QipPVD12HA5FBnjmiqVLphYgjfPtIPydn4Ie-xGKr=s1600-w200
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With