2016-11-13 92 views
2

我使用node.js並使用照片引用字符串並希望獲得對返回的圖像的訪問權限。處理谷歌照片引用以獲取圖像url

我有下面的代碼,正在取回正文中的東西。

locations.forEach(function(loc) { 
    var photoRef; 
    if (loc.photos && Array.isArray(loc.photos)) { 

     photoRef = loc.photos[0].photo_reference; 

     var url_in = 'https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=' + photoRef + '&key=' + key; 

     request(url_in, function (error, response, body) { 
     if (!error && response.statusCode == 200) { 
      console.log(body); // Show the HTML for the Google homepage. 
     } 
     }); 


    } 

    }); 

我只想得到字符串是圖像的URL。但是我得到的是代表網頁的對象。

任何幫助將不勝感激。

+0

你想要保存圖像,還是隻需要在網上獲取圖像的網址? –

+0

例如:從[此API調用(https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=CmRdAAAAHcsAE2aiv5lIxLEo0c9xMUmH-chXoAeq6yrFPw5uy7LdppAMxBmg8_yvG9y9qaXqsH-IrcPsBD6QQvsfZnpml3U5zgh93nTbm5WONnYmutBSltIr9FwuK-awm8l1vyKeEhD9AqZpJwFLIWms2GeZGPR2GhSPp1JyAU_6_s1Im1_oH5tB4IjyXQ&key=API-KEY)到這個[圖像URL] (https://lh3.googleusercontent.com/-B8FWOhhu2_k/VTdYJeFjRhI/AAAAAAAAAD8/JSJamjx4XyA/s1600-w400/) –

+0

@RohinGopalakrishnan我只是想圖像網址在線 – user1907509

回答

1

根據documentation獲取image url是不可能的,因爲url不會被API返回。

https://lh3.googleusercontent.com/-B8FWOhhu2_k/VTdYJeFjRhI/AAAAAAAAAD8/JSJamjx4XyA/s1600-w400/

image url但是已經

imageurl = 'https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=' + PHOTO_REFERENCE + '&key=' + API_KEY; 
// ^^^ this would return the image for you already (hence, your image url) 

您可以使用此圖片的URL爲每24小時1000個免費請求。通過在Google API控制檯上啓用結算功能,您可以免費增加此限制,每24小時可處理多達150,000個請求。

希望這有助於您!