地點相片

Places API 的 Place Photo 服務是唯讀 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.

相片參考

傳送至「地點相片」服務的所有要求都必須包含 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 欄位中包含值,您每次顯示圖片時就必須在應用程式中加入額外的作者資訊。

以下為 photos[] 陣列的範例。

...
"photos" : [
   {
      "html_attributions" : [],
      "height" : 853,
      "width" : 1280,
      "photo_reference" : "AUacShh3_Dd8yvV2JZMtNjjbbSbFhSv-0VmUN-uasQ2Oj00XB63irPTks0-A_1rMNfdTunoOVZfVOExRRBNrupUf8TY4Kw5iQNQgf2rwcaM8hXNQg7KDyvMR5B-HzoCE1mwy2ba9yxvmtiJrdV-xBgO8c5iJL65BCd0slyI1"
   }
...

地點相片範例

以下是要求範例。這項要求會傳回參照的圖片並調整大小,最大可達 400 像素。

https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photo_reference=AUacShh3_Dd8yvV2JZMtNjjbbSbFhSv-0VmUN-uasQ2Oj00XB63irPTks0-A_1rMNfdTunoOVZfVOExRRBNrupUf8TY4Kw5iQNQgf2rwcaM8hXNQg7KDyvMR5B-HzoCE1mwy2ba9yxvmtiJrdV-xBgO8c5iJL65BCd0slyI1&key=YOUR_API_KEY

地點相片回應

「地點相片」要求成功的回應為圖片。圖片類型則視原本提交的相片類型而定。

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

「超過配額」圖片

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

  • 所提交的相片參照不正確。
  • 要求不包含 maxwidthmaxheight 參數。
  • maxwidthmaxheight 參數的值已設為 null