如何检索photo_reference值以制作Google商家信息照片请求?

时间:2017-08-18 07:25:39

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

来自Google的Place Photos documentation我应该可以检索photo_reference密钥,向其附近的搜索,雷达或地方详细信息服务发送请求。

我正在点击他们的地方详细信息服务,我收到了包含10个元素的照片数组,但这些元素只包含heightwidthhtml_attribution个键。我在这里遗漏了什么或者在没有更新文档的情况下更改了API吗?

以下是我要求提供详细信息的请求,其中place是回复:

export function placeDetailsFetchData(placeId) {
  return (dispatch) => {
    const request = { placeId };
    const service = new google.maps.places.PlacesService(document.createElement('div'));

    service.getDetails(request, (place, status) => {
      if (status == google.maps.places.PlacesServiceStatus.OK) {
        dispatch({
          type: PLACE_DETAILS_FETCH_DATA_SUCCESS,
          place
        });
      } else {
        dispatch(placeDetailsHasErrored(true));
      }
    });
  }
}

以下是回复中的照片值:

[
  {
    "height": 3024,
    "html_attributions": [
      "<a href=\"https://maps.google.com/maps/contrib/109733975839515761188/photos\">Chris Bair</a>"
    ],
    "width": 4032
  },
  {
    "height": 2992,
    "html_attributions": [
      "<a href=\"https://maps.google.com/maps/contrib/100217631342523519461/photos\">Samir Soriano</a>"
    ],
    "width": 4000
  },
  {
    "height": 453,
    "html_attributions": [
      "<a href=\"https://maps.google.com/maps/contrib/106775786962992563502/photos\">Frisco Fried</a>"
    ],
    "width": 604
  },
  {
    "height": 5312,
    "html_attributions": [
      "<a href=\"https://maps.google.com/maps/contrib/111414489430298948350/photos\">Tu Lam</a>"
    ],
    "width": 2988
  },
  {
    "height": 1920,
    "html_attributions": [
      "<a href=\"https://maps.google.com/maps/contrib/109551371558914502695/photos\">Kenny Reed</a>"
    ],
    "width": 2560
  },
  {
    "height": 4160,
    "html_attributions": [
      "<a href=\"https://maps.google.com/maps/contrib/110844988640416794228/photos\">Vicious V</a>"
    ],
    "width": 3120
  },
  {
    "height": 2576,
    "html_attributions": [
      "<a href=\"https://maps.google.com/maps/contrib/103079141129202103200/photos\">Romayne Ward</a>"
    ],
    "width": 1932
  },
  {
    "height": 5312,
    "html_attributions": [
      "<a href=\"https://maps.google.com/maps/contrib/103167182117207780142/photos\">Michael C</a>"
    ],
    "width": 2988
  },
  {
    "height": 1836,
    "html_attributions": [
      "<a href=\"https://maps.google.com/maps/contrib/116997882870097389214/photos\">Sam Johnson</a>"
    ],
    "width": 3264
  },
  {
    "height": 4032,
    "html_attributions": [
      "<a href=\"https://maps.google.com/maps/contrib/113862636441079805689/photos\">Wes Wu</a>"
    ],
    "width": 3024
  }
]

1 个答案:

答案 0 :(得分:3)

google.maps.places.PlacePhoto对象提供了返回地方照片网址的方法getUrl()。使用此方法获取图片的网址,Maps JavaScript API不会公开照片参考。

查看文档以获取更多详细信息:

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

place.photos.forEach(function (placePhoto) {
    var url = placePhoto.getUrl({
        maxWidth: 600,
        maxHeight: 400
    });
});
相关问题