Google Places API网络服务显示了photo_reference,但Javascript库却没有

时间:2016-02-23 22:00:21

标签: google-places-api

我正在构建一个基本的地方搜索页面,我正在使用Google Places Javascript库(text search)。我回来了一个“照片”元素,但我注意到“photo_reference”字段为空。这是一个示例请求:

var service = new google.maps.places.PlacesService(this.map);
service.textSearch({query: 'harbour bridge'}, this.callback.bind(this));

Repsonse:

{
   "formatted_address":"Sydney Harbour Bridge, Sydney NSW, Australia",
   "geometry":{
      "location":{
         "lat":-33.8523063,
         "lng":151.21078710000006
      }
   },
   "icon":"https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
   "id":"e22913360d0b946d099c7a32a77a95e49f9ead66",
   "name":"Sydney Harbour Bridge",
   "photos":[{
     "height":4000,
     "html_attributions":["<a href=\"https://maps.google.com/maps/contrib/113178678511744469415/photos\">Jasper Straver</a>"],
     "width":6000
   }],
   "place_id":"ChIJ49XqJV2uEmsRPsTAF7eOlGg",
   "rating":4.7,
   "reference":"[...]",
   "types":["point_of_interest", "establishment"],
   "html_attributions":[]
}

如果我使用网络服务执行相同的请求,我会获得photo_reference。例如:

https://maps.googleapis.com/maps/api/place/textsearch/json?key=[...]&query=harbour%20bridge

响应:

{
     "formatted_address" : "Sydney Harbour Bridge, Sydney NSW, Australia",
     "geometry" : {
        "location" : {
           "lat" : -33.8523063,
           "lng" : 151.2107871
        }
     },
     "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
     "id" : "e22913360d0b946d099c7a32a77a95e49f9ead66",
     "name" : "Sydney Harbour Bridge",
     "photos" : [
        {
           "height" : 4000,
           "html_attributions" : ["\u003ca href=\"https://maps.google.com/maps/contrib/113178678511744469415/photos\"\u003eJasper Straver\u003c/a\u003e"],
           "photo_reference" : "CmRdAAAAY-WUame_CRFnMFmUN4UlvFHI7o3tQOqXJxTkjQINgzMQOOheBzLPIm43dlIAIkhFyugFAw8fnf-ItEiUp1j48B23sCDFRtCWM123euhDif_P1jYkvFAjDrPxq1rCnmi2EhCt6LpVl5W-AKPLRkW_tzs6GhRlG4dx2CVuTNZZMFFo3eYMSFWzGg",
           "width" : 6000
        }
     ],
     "place_id" : "ChIJ49XqJV2uEmsRPsTAF7eOlGg",
     "rating" : 4.7,
     "reference" : "[...]",
     "types" : [ "point_of_interest", "establishment" ]
  }

JS和Web Services API之间有什么区别?如何从JS库中获取photo_reference? (我不想为每个搜索结果发出getDetails(...)请求)

谢谢!

1 个答案:

答案 0 :(得分:1)

photo_reference适用于Google Places API 网络服务。使用Places Javascript库,您只需在photos数组的元素上调用getUrl,例如place.photos[0].getUrl({maxWidth: 1000})

与网络服务一样,搜索结果的照片不会超过1张。