地點相片

「地點相片」服務是 Places API 的一部分,這個唯讀 API 可讓您在應用程式中加入高品質的相片內容。「地點相片」服務可讓您存取儲存在地點介面集資料庫中數百萬張的相片。使用 Place Details 要求取得地點資訊時,系統會傳回相片參照,提供您相關的影像內容。Find Place、Nearby Search 和 Text Search 要求也會視情況傳回每個地點的單一相片參照。之後,您可以使用「地點相片」服務存取參照的相片,並將圖片調整為適合應用程式的尺寸。

地點相片要求

「地點相片」要求是一個 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.

相片參考資源

對 Place Photo 服務發出的所有要求都必須包含 photo_reference,然後會傳回給 Find Place、Nearby Search、Text Search 或 Place Details 要求的回應。如果該地點有相關圖像內容,這些要求的回應會包含 photos[] 欄位。

注意:系統傳回的相片數量會因要求而異。

  • Find Place、Nearby Search 或 Text Search 要求最多會傳回陣列中的一個 photo 元素。
  • Place Details 要求最多會傳回十個 photo 元素。

每個 photo 元素都包含下列欄位:

  • photo_reference:執行相片要求時,用於識別相片的字串。
  • height:圖片的高度上限。
  • width:圖片的寬度上限。
  • html_attributions[]:包含所有必要的作者資訊。系統一律會顯示這個欄位,但可能會留空。

「地點相片」服務傳回的相片取自各種地點,包括業主和使用者提供的相片。在大多數情況下,使用這些相片時可以不包含作者資訊,圖片本身也可能已加入必要的作者資訊。不過,如果傳回的 photo 元素在 html_attributions 欄位中包含值,您每次顯示圖片時就必須在應用程式中加入其他屬性。

以下範例顯示 Place Details 要求:

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

Place Photo 回應

「地點相片」要求成功的回應將為圖片。圖片類型取決於原本提交的相片的類型。

如果您的要求超過可用配額,伺服器會傳回 HTTP 403 狀態並顯示下圖,說明已超出配額:

「超過配額」的圖片

如果伺服器無法理解您的要求,會傳回 HTTP 400 狀態,表示要求無效。無效的要求最常見原因包括:

  • 提交的相片參照不正確。
  • 要求中未包含 maxwidthmaxheight 參數。
  • maxwidthmaxheight 參數的值已設為 null
  • photo_reference已過期。如果 photo_reference 過期,請向 Place DetailsFind PlaceNearby SearchText Search 要求取得更新後的 photo_reference 值。