从 google 地图 api 获取照片

Get Photos from google map api

我想取回 Google 地图 api 的照片并将它们插入 DOM。在文档中,它根据谁进行编码允许取回几个地方的照片并将它们用作标记

function createPhotoMarker(place) {
var photos = place.photos;
if (!photos) {
return;
}

var marker = new google.maps.Marker({
map: map,
position: place.geometry.location,
title: place.name,
icon: photos[0].getUrl({'maxWidth': 35, 'maxHeight': 35})
});
}

效果很好,但你能告诉我如何找到照片并将其插入 DOM 吗? 我已经尝试过类似的东西,但他不是一个好方法:我的测试。抱歉我的英语不好

 function callback(results, status) {
                if (status === google.maps.places.PlacesServiceStatus.OK) {
                    for (var i = 0; i < results.length; i++) {
                        createMarker(results[i]);
                        restos.push(results[i].name);
                        // console.log(results[i]);
                        var btnSuite = document.createElement('button');
                        createPhoto(results[i]); //photos test !!??
                        btnSuite.innerHTML = '<b>' + results[i].name +      '</b><br>' + results[i].vicinity + '<br>' + '<img src=' + createPhoto(results[i]) + '/>';

                        btnSuite.id = 'droiteBtns';
                        droite2.appendChild(btnSuite);



                    }

                }

            }
 function createPhoto(place) {
                var photos = place.photos;
                if (!photos) {
                    return;
                }

                var photo = photos[0].getUrl({
                    'maxWidth': 150,
                    'maxHeight': 150
                })

            }

感谢您的帮助

你可以试试这个: http://jsfiddle.net/mervinsamy/m1ejzt7j/

我所做的是创建一个 img 对象,将图像存储在那里,然后将其添加到现有的 div。

相关HTML:

<div class="photos-section" id="img-container"></div>

相关JS:

var img = document.createElement("img") //Create object
img.src = photos[0].getUrl({'maxWidth': 100, 'maxHeight': 100}); //store image url as src attribute
document.getElementById("img-container").appendChild(img);  //add to existing div