簡體   English   中英

Google Places API庫未返回photo_reference屬性

[英]Google Places API library is not returning photo_reference attribute

我正在使用Google Places API來獲取某個地點的詳細信息,但它沒有返回照片的photo_reference屬性。 為什么??

這是我如何稱呼它:

  var request = {
    placeId: 'ChIJN1t_tDeuEmsRUsoyG83frY4'
  };

  service = new google.maps.places.PlacesService(document.createElement('div'));
  service.getDetails(request, callback);

  function callback(placeD, status) {
    console.log( placeD );
    console.log( JSON.stringify(placeD) );
    if (status == google.maps.places.PlacesServiceStatus.OK) {
      console.log( placeD );
    }
  }

這是回應

{
   "address_components":[
      {
         "long_name":"5",
         "short_name":"5",
         "types":[
            "floor"
         ]
      },
      {
         "long_name":"48",
         "short_name":"48",
         "types":[
            "street_number"
         ]
      },
      {
         "long_name":"Pirrama Road",
         "short_name":"Pirrama Rd",
         "types":[
            "route"
         ]
      },
      {
         "long_name":"Pyrmont",
         "short_name":"Pyrmont",
         "types":[
            "locality",
            "political"
         ]
      },
      {
         "long_name":"Council of the City of Sydney",
         "short_name":"Sydney",
         "types":[
            "administrative_area_level_2",
            "political"
         ]
      },
      {
         "long_name":"New South Wales",
         "short_name":"NSW",
         "types":[
            "administrative_area_level_1",
            "political"
         ]
      },
      {
         "long_name":"Australia",
         "short_name":"AU",
         "types":[
            "country",
            "political"
         ]
      },
      {
         "long_name":"2009",
         "short_name":"2009",
         "types":[
            "postal_code"
         ]
      }
   ],
   "adr_address":"5, <span class=\"street-address\">48 Pirrama Rd</span>, <span class=\"locality\">Pyrmont</span> <span class=\"region\">NSW</span> <span class=\"postal-code\">2009</span>, <span class=\"country-name\">Australia</span>",
   "formatted_address":"5, 48 Pirrama Rd, Pyrmont NSW 2009, Australia",
   "formatted_phone_number":"(02) 9374 4000",
   "geometry":{
      "location":{
         "lat":-33.8666113,
         "lng":151.19583239999997
      },
      "viewport":{
         "south":-33.8669293,
         "west":151.19521829999996,
         "north":-33.86650529999999,
         "east":151.1960371
      }
   },
   "icon":"https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
   "id":"4f89212bf76dde31f092cfc14d7506555d85b5c7",
   "international_phone_number":"+61 2 9374 4000",
   "name":"Google",
   "opening_hours":{
      "open_now":false,
      "periods":[
         {
            "close":{
               "day":1,
               "time":"1800",
               "hours":18,
               "minutes":0,
               "nextDate":1468828800000
            },
            "open":{
               "day":1,
               "time":"0800",
               "hours":8,
               "minutes":0,
               "nextDate":1468792800000
            }
         },
         {
            "close":{
               "day":2,
               "time":"1800",
               "hours":18,
               "minutes":0,
               "nextDate":1468915200000
            },
            "open":{
               "day":2,
               "time":"0800",
               "hours":8,
               "minutes":0,
               "nextDate":1468879200000
            }
         },
         {
            "close":{
               "day":3,
               "time":"1800",
               "hours":18,
               "minutes":0,
               "nextDate":1469001600000
            },
            "open":{
               "day":3,
               "time":"0800",
               "hours":8,
               "minutes":0,
               "nextDate":1468965600000
            }
         },
         {
            "close":{
               "day":4,
               "time":"1800",
               "hours":18,
               "minutes":0,
               "nextDate":1469088000000
            },
            "open":{
               "day":4,
               "time":"0800",
               "hours":8,
               "minutes":0,
               "nextDate":1469052000000
            }
         },
         {
            "close":{
               "day":5,
               "time":"1800",
               "hours":18,
               "minutes":0,
               "nextDate":1469174400000
            },
            "open":{
               "day":5,
               "time":"0800",
               "hours":8,
               "minutes":0,
               "nextDate":1469138400000
            }
         }
      ],
      "weekday_text":[
         "Monday: 8:00 AM – 6:00 PM",
         "Tuesday: 8:00 AM – 6:00 PM",
         "Wednesday: 8:00 AM – 6:00 PM",
         "Thursday: 8:00 AM – 6:00 PM",
         "Friday: 8:00 AM – 6:00 PM",
         "Saturday: Closed",
         "Sunday: Closed"
      ]
   },
   "photos":[
      {
         "height":1365,
         "html_attributions":[
            "<a href=\"https://maps.google.com/maps/contrib/105932078588305868215/photos\">Maksym Kozlenko</a>"
         ],
         "width":2048
      },
      {
         "height":3120,
         "html_attributions":[
            "<a href=\"https://maps.google.com/maps/contrib/116783625253584801301/photos\">Samson Jabin</a>"
         ],
         "width":4160
      },
      {
         "height":1184,
         "html_attributions":[
            "<a href=\"https://maps.google.com/maps/contrib/106645265231048995466/photos\">Malik Ahamed</a>"
         ],
         "width":1776
      },
      {
         "height":960,
         "html_attributions":[
            "<a href=\"https://maps.google.com/maps/contrib/100919424873665842845/photos\">Donnie Piercey</a>"
         ],
         "width":1280
      },
      {
         "height":3024,
         "html_attributions":[
            "<a href=\"https://maps.google.com/maps/contrib/117393076859685917096/photos\">Joann Chu</a>"
         ],
         "width":4032
      },
      {
         "height":5582,
         "html_attributions":[
            "<a href=\"https://maps.google.com/maps/contrib/110754641211532656340/photos\">Robert Koch</a>"
         ],
         "width":2866
      },
      {
         "height":2988,
         "html_attributions":[
            "<a href=\"https://maps.google.com/maps/contrib/103594026264127040754/photos\">Daniel Tyson</a>"
         ],
         "width":5312
      },
      {
         "height":2448,
         "html_attributions":[
            "<a href=\"https://maps.google.com/maps/contrib/116976377324210679577/photos\">WH CHEN</a>"
         ],
         "width":3264
      },
      {
         "height":1184,
         "html_attributions":[
            "<a href=\"https://maps.google.com/maps/contrib/106645265231048995466/photos\">Malik Ahamed</a>"
         ],
         "width":1776
      },
      {
         "height":4160,
         "html_attributions":[
            "<a href=\"https://maps.google.com/maps/contrib/116783625253584801301/photos\">Samson Jabin</a>"
         ],
         "width":3120
      }
   ],
   "place_id":"ChIJN1t_tDeuEmsRUsoyG83frY4",
   "rating":4.6,
   "reference":"CmRaAAAAXa9Pjme9u0GwglGBnp5nlj_r92tKtn3rY9mx22RTSHhZSNnYd3RvNVzl33PB_7qyhodZq_KkKx2KH9JIB0NYXuUNgVz2UKO_OMakrvJGVNrJ2Y6ue78-OVBtO6y5NdE0EhBCQrgjqWR2H2rv2ZWZrmPcGhT_f4ZFK2OYBor8uimEqGIwtBTfew",
   "reviews":[
      {
         "aspects":[
            {
               "rating":3,
               "type":"overall"
            }
         ],
         "author_name":"Nila Sweeney",
         "author_url":"https://plus.google.com/115167791509064907883",
         "language":"en",
         "profile_photo_url":"//lh4.googleusercontent.com/-P-5LDZmJbig/AAAAAAAAAAI/AAAAAAAAvAY/Df_n6XR0I2I/photo.jpg",
         "rating":5,
         "text":"Amazing place!  So incredibly vibrant and hip. I love the way the office have been laid out. The way work-life balance is encouraged is truly impressive!  Google, please hire me! ",
         "time":1464213084
      },
      {
         "aspects":[
            {
               "rating":0,
               "type":"overall"
            }
         ],
         "author_name":"TheHealthRetreat",
         "author_url":"https://plus.google.com/110246682709687277403",
         "language":"en",
         "rating":1,
         "text":"I spend 250 thousand dollars a year with Google adwords..\nI own a Mental Health Drug and alcohol retreat.\n\nA fictitious person Antony William posted a negative review on my Business.\n\nThis person has never attended.  I asked Google for the last 8 weeks to remove the third party review, I also sent 20 times a statutory declaration to googlemybusiness then goodle reviews and google legal stating Mr Antony had never attended the program.\n\nGoogle have refused to remove the review even though it clearly breaches Googles own guidelines and the post is defamatory.\nGoogle is enabling this person to continue to hurt our business by 100000 per month since the post went up.\n\nMy request for the post to be removed is being stone walled by google.\n\nI cannot believe that Google would treat their business partners so shabbily when they are being wronged by an anonymous person or I believe competition business.\nGoogle says over and over negotiate with Antony William,  how can I negotiate with someone who doesn't exist?  It is frustrating and plain wrong.\n\nIf Google does this to a client who spends 250000 ausd a year with them, god help us all.\n\nBy the way if you have depression and anxiety dealing with Google, come to us and we will help you. regards Francis",
         "time":1468373176
      },
      {
         "aspects":[
            {
               "rating":0,
               "type":"overall"
            }
         ],
         "author_name":"Steve Morgan",
         "author_url":"https://plus.google.com/103384372688674733198",
         "language":"en",
         "profile_photo_url":"//lh5.googleusercontent.com/-xCUbmlcbDas/AAAAAAAAAAI/AAAAAAAAIAE/OLWRkcph3xs/photo.jpg",
         "rating":1,
         "text":"Latest Andriod update 5.0 has broken Google  Play for the Samsung Note  3. No Gmail Apo no fitbit exercise  logging.  Absolutely  disgusting.  Almost sebds ne to Apple",
         "time":1468325696
      },
      {
         "aspects":[
            {
               "rating":3,
               "type":"overall"
            }
         ],
         "author_name":"Keith Hong",
         "author_url":"https://plus.google.com/106314339785578978358",
         "language":"en",
         "profile_photo_url":"//lh3.googleusercontent.com/-Z69cMMxhr-c/AAAAAAAAAAI/AAAAAAAAAEs/CbphM5oqKas/photo.jpg",
         "rating":5,
         "text":"Not often you get invited to the google Australia HQ. Neat spot. Love their Pirrama place but their ODI location is pretty cool too (and soon overtakin most of Fairfax) ",
         "time":1464676368
      },
      {
         "aspects":[
            {
               "rating":3,
               "type":"overall"
            }
         ],
         "author_name":"Alejandro Silva Avila",
         "author_url":"https://plus.google.com/102339092633102421576",
         "language":"en",
         "profile_photo_url":"//lh5.googleusercontent.com/-WRvjv4eUHjY/AAAAAAAAAAI/AAAAAAAAUsk/6UjJkZU6U9w/photo.jpg",
         "rating":5,
         "text":"Completely stunning and different from anything you'd imagine. So many cool things to see and do you'd think people get lost trying to do some work haha! :)",
         "time":1467896264
      }
   ],
   "scope":"GOOGLE",
   "types":[
      "point_of_interest",
      "establishment"
   ],
   "url":"https://maps.google.com/?cid=10281119596374313554",
   "utc_offset":600,
   "vicinity":"5 48 Pirrama Road, Pyrmont",
   "website":"https://www.google.com.au/about/careers/locations/sydney/",
   "html_attributions":[

   ]
}

如果我使用此URL查看同一個地方的詳細信息,我可以獲取photo_reference屬性:

https://maps.googleapis.com/maps/api/place/details/json?reference=CmRaAAAAXa9Pjme9u0GwglGBnp5nlj_r92tKtn3rY9mx22RTSHhZSNnYd3RvNVzl33PB_7qyhodZq_KkKx2KH9JIB0NYXuUNgVz2UKO_OMakrvJGVNrJ2Y6ue78-OVBtO6y5NdE0EhBCQrgjqWR2H2rv2ZWZrmPcGhT_f4ZFK2OYBor8uimEqGIwtBTfew&sensor=false&key=AIzaSyAmUTQw6a8sQESv77boQhf3tvTsaAJy09c

使用javscript庫時,照片對象有一個方法“getUrl”而不是photo_reference。 這有點令人困惑,但你可以循環這些圖像並使用maxHeight和/或maxWidth調用該方法(至少其中一個必須存在)並且它將返回一個照片網址。

https://developers.google.com/maps/documentation/javascript/3.exp/reference#PlacePhoto

function callback(placeD, status) {
    console.log( placeD );
    console.log( JSON.stringify(placeD) );
    if (status == google.maps.places.PlacesServiceStatus.OK) {
        placeD.photos.forEach(photo => {
            console.log(photo.getUrl({maxHeight: 300})) // will log a url but no photo_reference
        })
    }    
}

不幸的是,我還沒有想出如何從javascript庫獲取photo_reference。

您需要從Place SearchPlace Details請求中獲取照片參考。

Place Photo服務是Google Places API Web服務的一部分,是一個只讀API,可讓您為應用程序添加高質量的照片內容。

當您使用地方詳情請求獲取地點信息時,將返回相關照片內容的照片參考。

到地方照片服務的所有請求都必須包括一個photoreference 它是唯一標識照片的字符串標識符。 照片參考從地方搜索或地方詳細信息請求返回。

photos[]數組的示例如下所示。

...

"photos" : [
{
"html_attributions" : [],
"height" : 853,
"width" : 1280,
"photo_reference" : "CnRvAAAAwMpdHeWlXl-lH0vp7lez4znKPIWSWvgvZFISdKx45AwJVP1Qp37YOrH7sqHMJ8C-vBDC546decipPHchJhHZL94RcTUfPa1jWzo-rSHaTlbNtjh-N68RkcToUCuY9v2HNpo5mziqkir37WU8FJEqVBIQ4k938TI3e7bf8xq-uwDZcxoUbO_ZJzPxremiQurAYzCTwRhE_V0"
}
...

地方照片請求是以下格式的HTTP URL:

https://maps.googleapis.com/maps/api/place/photo?parameters

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM