【问题标题】:Get Photos from google map api从谷歌地图 api 获取照片
【发布时间】:2018-05-29 21:04:58
【问题描述】:

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

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

            }

感谢您的帮助

【问题讨论】:

    标签: javascript google-maps-api-3 google-places-api


    【解决方案1】:

    你可以试试这个: 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
    

    【讨论】:

    • 好的,我理解我的错误。非常感谢帮助
    • 在我的回调函数中,我有 ID 为“droiteBtns”的按钮,我的结果是多个按钮,我可以使用 createPhoto(results[i]) 在按钮内插入照片?
    • 我在 createPhoto() 中找到了解决方案,我使用我的按钮为 each() 执行此操作。再次感谢@MervinSamy
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-13
    • 2012-01-17
    • 1970-01-01
    • 2017-08-29
    • 2013-10-24
    相关资源
    最近更新 更多