地点照片(旧版)

地点照片(旧版)是 Places API 的一部分,是一个只读 API,可让您向应用添加高品质照片内容。借助地点照片(旧版),您可以访问存储在地点数据库中的数百万张照片。当您使用“地点详情”请求获取地点信息时,系统将返回相关照片内容的照片引用。“查找地点(旧版)”“附近搜索(旧版)”和“文本搜索(旧版)”请求也会为每个地点返回一个照片引用(如果相关)。然后,您可以使用 Google 相册地点(旧版)访问所引用的照片,并根据自己的应用将图片调整为最适合的大小。

“地点照片(旧版)”请求

地点照片(旧版)请求是采用以下格式的 HTTP 网址:

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

您需要使用某些参数,才能发起搜索请求。依照网址的标准,所有参数都使用“与”符号 (&) 分隔。下面列出了参数及其可能值的列表。

Required parameters

  • photo_reference

    A string identifier that uniquely identifies a photo. Photo references are returned from either a Place Search or Place Details request.

  • maxheight or maxwidth

    maxheight specifies the maximum desired height, in pixels, of the image. If the image is smaller than the values specified, the original image will be returned. If the image is larger in either dimension, it will be scaled to match the smaller of the two dimensions, restricted to its original aspect ratio.

    maxwidth specifies the maximum desired width, in pixels, of the image. If the image is smaller than the values specified, the original image will be returned. If the image is larger in either dimension, it will be scaled to match the smaller of the two dimensions, restricted to its original aspect ratio.

    Both the maxheight and maxwidth properties accept an integer between 1 and 1600.

    You must specify either maxheight, or maxwidth, or both.

照片参考

对“地点照片(旧版)”的所有请求都必须包含 photo_reference,该值会在“查找地点(旧版)”“附近搜索(旧版)”“文本搜索(旧版)”或“地点详情(旧版)”请求的响应中返回。如果相应地点有相关的照片内容,这些请求的响应将包含 photos[] 字段。

注意:返回的照片数量因请求而异。

  • “查找地点(旧版)”“附近搜索(旧版)”或“文本搜索(旧版)”请求最多会在数组中返回一个 photo 元素。
  • “地点详情(旧版)”请求最多会返回十个 photo 元素。

每个 photo 元素都包含以下字段:

  • photo_reference - 用于在您执行“地点照片(旧版)”请求时标识照片的字符串。
  • height - 图片的最大高度。
  • width - 图片的最大宽度。
  • html_attributions[] - 包含所有必需的提供方信息。此字段始终存在,但可能为空。

地点照片(旧版)返回的照片来自各种位置,包括商家和用户贡献的照片。在大多数情况下,使用这些照片时可以不包含提供方说明,或者可以在图片中显示必要的提供方说明。但是,如果返回的 photo 元素在 html_attributions 字段中包含值,无论您在哪里显示相应图片,都要在应用中包含额外的提供方说明。

以下示例展示了“地点详情(旧版)”请求:

curl "https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJcUElzOzMQQwRLuV30nMUEUM&key=YOUR_API_KEY"

下面显示了响应中的 photos[] 数组示例。

    ...
    "photos" : [
       {
          "html_attributions" : [],
          "height" : 853,
          "width" : 1280,
          "photo_reference" : "ATJ83zhSSAtkh5LTozXMhBghqubeOxnZWUV2m7Hv2tQaIzKQJgvZk9yCaEjBW0r0Zx1oJ9RF1G7oeM34sQQMOv8s2zA0sgGBiyBgvdyMxeVByRgHUXmv-rkJ2wyvNv17jyTSySm_-_6R2B0v4eKX257HOxvXlx_TSwp2NrICKrZM2d5d2P4q"
       }]
    ...

地点照片(旧版)示例

示例请求如下所示。此请求会返回引用的图片,并将其调整为宽度不超过 400 像素。

https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photo_reference=ATJ83zhSSAtkh5LTozXMhBghqubeOxnZWUV2m7Hv2tQaIzKQJgvZk9yCaEjBW0r0Zx1oJ9RF1G7oeM34sQQMOv8s2zA0sgGBiyBgvdyMxeVByRgHUXmv-rkJ2wyvNv17jyTSySm_-_6R2B0v4eKX257HOxvXlx_TSwp2NrICKrZM2d5d2P4q&key=YOUR_API_KEY

地点照片(旧版)响应

成功地点照片(旧版)请求的响应是图片。图片的类型取决于最初提交的照片的类型。

如果您的请求超出了可用配额,服务器会返回 HTTP 403 状态,并显示以下图片以指明已超出配额:

“超出配额”图片

如果服务器无法理解您的请求,则会返回 HTTP 400 状态,表示请求无效。请求无效的最常见原因包括: