Show image from Google API Place Photo response

荒凉一梦 提交于 2021-01-29 04:38:52

问题


I am working with Meteor.js. I need place photos of google place. I am working with Javascript here. So here is what I've done.

            Meteor.call('getPlaceDetails', result.place_id, function (error, placeDetailsResult) {
                if (error) {
                    console.log(error);
                } else {
                    console.log(placeDetailsResult.data.result.photos[0].photo_reference);

                    Meteor.call('getPlacePhotos', placeDetailsResult.data.result.photos[0].photo_reference, function (error, photoresult) {
                        if (error) {
                            console.log(error);
                        } else {
                            console.log(photoresult);
                        }
                    });

                }
            });

I successfully got place_id from Place Details API call. With place_id I can then again successfully call the Place Photos API. I think all goes well here. In response I should get the photo and this is my responded JSON object:

 Object {statusCode: 200, content: "����JFIF��*ExifII*1…!�o~Ç����`��&]<sP�\U��TV-���@#�{��8�#7��*�"���", headers: Object, data: null}
content
:
"����JFIF��*ExifII*1Google���↵  ↵↵↵↵↵
data
:
null
headers
:
Object
access-control-allow-origin
:
"*"
access-control-expose-headers
:
"Content-Length"
alt-svc
:
"quic=":443"; ma=2592000; v="36,35,34,33,32,31,30""
alternate-protocol
:
"443:quic"
cache-control
:
"public, max-age=86400, no-transform"
connection
:
"close"
content-disposition
:
"inline;filename="2015-11-13.jpg""
content-length
:
"38587"
content-type
:
"image/jpeg"
date
:
"Thu, 01 Sep 2016 12:12:47 GMT"
etag
:
""v21fad""
expires
:
"Fri, 02 Sep 2016 12:12:47 GMT"
server
:
"fife"
vary
:
"Origin"
x-content-type-options
:
"nosniff"
x-xss-protection
:
"1; mode=block"
__proto__
:
Object
statusCode
:
200
__proto__
:
Object

In the documentation here: https://developers.google.com/places/web-service/photos they say you get a photo in return. So everything is fine, but I don't know how to display this photo on my website according there is no url. Please provide some usefull info.

Thanks :)


回答1:


I did miss that function of what Sorin Lascu has commented. If someone is doing that in Meteor.js I will provide a complete and easy answer. I created an input field which has Google Maps API autocomplete, and shows the place on a map instantly. You also get all the necessary data you need to show to photos and other details.

I added this package jeremy:geocomplete to my project.

I created a map in HTML like this:

<div class="col s12 m10 l10 push-m1 push-l1" id="google_mapPlace">
    {{> googleMap name="mapPlace" options=mapOptions}}
</div>

I added an input field in HTML

<input value="" type="text" class="findPlace">

Then in my js file I update my function with autorun.

Template.adminCollections.onRendered(function() {
    this.autorun(function () {
        if (GoogleMaps.loaded()) {
            $(".findPlace").geocomplete({
                map: "#google_mapPlace",
                nearbySearchKeys: ['photos', 'place_id', 'name', 'geometry']
            }).bind("geocode:result", function(event, result){
                $('.myimg').attr('src', result.photos[0].getUrl({'maxWidth': 500, 'maxHeight': 500}));
            });
        });
    });
});


来源:https://stackoverflow.com/questions/39271759/show-image-from-google-api-place-photo-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!