Google Maps API从附近的搜索JSON获取photo_reference

时间:2015-06-02 20:12:38

标签: json google-maps

早上好,

目前我正在尝试建立一个网站,该网站在谷歌地图API中显示来自附近搜索的一张照片。在这个例子中,我只想尝试使用photo_reference,我需要稍后显示照片,但我无法弄清楚如何获得这个photo_reference ID。这是我目前没有工作的代码:

var url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&types=food&name=cruise&key=MYAPIKEY";
$.getJSON(url, function (data) {
    var photo_reference = data.results[0].photos[0].photo_reference;
    alert(photo_reference);

});

我希望那里的人能帮助我解决这个问题。

以下是我想要的照片参考:

{
   "html_attributions" : [],
 "results" : [
     {
     "geometry" : {
        "location" : {
           "lat" : -33.86879,
           "lng" : 151.194217
        }
     },
     "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
     "id" : "21a0b251c9b8392186142c798263e289fe45b4aa",
     "name" : "Rhythmboat Cruises",
     "opening_hours" : {
        "open_now" : false,
        "weekday_text" : []
     },
     "photos" : [
        {
           "height" : 467,
           "html_attributions" : [],
           "photo_reference" : "CnRnAAAAUYdlxXeaepO7jaUN863gNM1ExtUmabHALXdL1hsrpH6bWVtbXo_3ZXRz--oVGXBYnZJr8M5IMWqtLYDoUPFSNwxrJjWTRnZMPCsuEXbMOFaHNfF1xBwGEAcV-SkV0qorkv7b0jeT_CP0dMXND-H3jhIQft0a9rAxYMhQob9VALAPSRoUrRCcxHa7DdVxbxd8FwOH7UL_JJQ",
           "width" : 573
        }
     ],

编辑:它似乎只适用于HTTP而不是HTTPS ...不寻找解决方法。

2 个答案:

答案 0 :(得分:0)

您可以参考开发人员指南:https://developers.google.com/places/webservice/photos#place_photo_requests

建议你可以通过以下方式获取照片:

  

https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=CnRtAAAATLZNl354RwP_9UKbQ_5Psy40texXePv4oAlgP4qNEkdIrkyse7rPXYGd9D_Uj1rVsQdWT4oRz4QrYAJNpFX7rzqqMlZw2h2E2y5IKMUZ7ouD_SlcHxYq1yL4KbKUv3qtWgTK0A6QbGh87GB3sscrHRIQiG2RrmU_jF4tENr9wGS_YxoUSSDrYjWmrNfeEHSGSc3FyhNLlBU&key=API_KEY

= - = - = - = - = - 编辑 - = - = - = - = - =
您不应使用客户端网络应用中的Google Maps API Web Services,因为它违反了Access-Control-Allow-Origin政策。

正确的方法是使用Places Library in Google Maps Javascript API原样shown here

答案 1 :(得分:-2)

    String response = String.Empty;
    string api = https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=CnRtAAAATLZNl354RwP_9UKbQ_5Psy40texXePv4oAlgP4qNEkdIrkyse7rPXYGd9D_Uj1rVsQdWT4oRz4QrYAJNpFX7rzqqMlZw2h2E2y5IKMUZ7ouD_SlcHxYq1yL4KbKUv3qtWgTK0A6QbGh87GB3sscrHRIQiG2RrmU_jF4tENr9wGS_YxoUSSDrYjWmrNfeEHSGSc3FyhNLlBU&key=API_KEY;

    HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(api);
    WebResponse myResp = myReq.GetResponse();
    StreamReader reader = new StreamReader(myResp.GetResponseStream());
    reader.ReadToEnd();
    response = Newtonsoft.Json.JsonConvert.SerializeObject(myResp.ResponseUri.AbsoluteUri);

Now you will get image url.