Google Places API库未返回photo_reference属性

时间:2016-07-15 18:00:11

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

我正在使用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":[

   ]
}

如果我使用此网址查看同一地点的详细信息,我可以获取photo_reference属性:

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

2 个答案:

答案 0 :(得分:3)

使用javscript库时,照片对象有一个方法&#34; getUrl&#34;而不是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。

答案 1 :(得分:-3)

您需要从Place SearchPlace Details请求中获取照片参考。

Google Places API网络服务的Place Photo service是一个只读API,可让您为应用添加高质量的照片内容。

当您使用地方详情请求获取地点信息时,将返回相关照片内容的照片参考。

对Place Photo服务的所有请求必须包含photoreference。它是唯一标识照片的字符串标识符。照片参考从地方搜索或地方详情请求中返回。

photos[]数组的示例如下所示。

...

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

地方照片请求是以下格式的HTTP网址:

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