簡體   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