繁体   English   中英

从Google Map API获取照片

[英]Get Photos from google map api

我想取回Google Map 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})
});
}

效果很好bur您能告诉我如何找到照片并将其插入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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM