尤川豪   ·  4年前
445 貼文  ·  275 留言

關於 Google 地圖照片API 的計費以及使用政策研究

正在開發某服務,想撈 Google 地圖上的照片

流程

  1. 使用 google 提供的 client id 與 secret,根據地理位置撈到一串照片的 photo_reference
  2. 使用 photo_reference 與 api key 組成新網址,新網址會跳轉到圖片的網址(網址會是 *.googleusercontent.com)

價格

根據價目表 https://cloud.google.com/maps-platform/pricing/sheet/?hl=zh-tw

places photo 的費用是每千次 $7.00 美元,相當於每開一次照片,就要收費 0.2 新台幣!

這實在是非常貴!

疑問

因為流程第二步的新網址可以直接在 html 內放進 img src,資料庫該存跳轉前還是跳轉後的資料?

存跳轉前的話,等於每次用戶打開圖片都透過 photo_reference 去得到圖片一次。

這是正確用法嗎?API 如此昂貴?

存跳轉後的話就好很多,只有第一次解析時會被計費。但是,這跳轉後的網址會不會過期?

Google 是不是本來就想每次讀取圖片都收錢?正確的使用政策很難找,該怎麼辦?

官方原始碼

查找官方 library 的使用範例

https://github.com/googlemaps/google-maps-services-python/blob/master/googlemaps/places.py

def places_photo(client, photo_reference, max_width=None, max_height=None):
    """
    Downloads a photo from the Places API.
    :param photo_reference: A string identifier that uniquely identifies a
        photo, as provided by either a Places search or Places detail request.
    :type photo_reference: string
    :param max_width: Specifies the maximum desired width, in pixels.
    :type max_width: int
    :param max_height: Specifies the maximum desired height, in pixels.
    :type max_height: int
    :rtype: iterator containing the raw image data, which typically can be
        used to save an image file locally. For example:

        f = open(local_filename, 'wb')
        for chunk in client.places_photo(photo_reference, max_width=100):
            if chunk:
                f.write(chunk)
        f.close()

    """

    if not (max_width or max_height):
        raise ValueError("a max_width or max_height arg is required")

    params = {"photoreference": photo_reference}

    if max_width:
        params["maxwidth"] = max_width
    if max_height:
        params["maxheight"] = max_height

    # "extract_body" and "stream" args here are used to return an iterable
    # response containing the image file data, rather than converting from
    # json.
    response = client._request("/maps/api/place/photo", params,
                           extract_body=lambda response: response,
                           requests_kwargs={"stream": True})
    return response.iter_content()

所以官方範例根本是把圖片載下來,表示 Google 無意讓用戶每次打開你的圖片都要收費,只是透過 photo_reference 查找時要收費而已。這樣合理多了,嚇我一跳,虛驚一場。

  分享   共 2,132 次點閱
按了喜歡:
共有 0 則留言
還沒有人留言。歡迎分享您的觀點、或是疑問。
您的留言
尤川豪
445 貼文  ·  275 留言

Devs.tw 是讓工程師寫筆記、網誌的平台。隨手紀錄、寫作,方便日後搜尋!

歡迎您一起加入寫作與分享的行列!

查看所有文章