Geocoding API 和 Place API 都是用來查地點的 API。只是這兩個相似的 API 卻有陷阱:
以 cURL 來執行的話,兩者分別:
Geocoding API |
Place API |
curl -i "https://maps.googleapis.com/maps/api/geocode/json?address=taipei+101&key=[Your API Key]"
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Date: Wed, 15 Dec 2021 04:21:20 GMT
Pragma: no-cache
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Cache-Control: no-cache, must-revalidate
Access-Control-Allow-Origin: *
Server: mafe
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
Server-Timing: gfet4t7; dur=542
Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
Accept-Ranges: none
Vary: Accept-Language,Accept-Encoding
Transfer-Encoding: chunked
|
curl -i "https://maps.googleapis.com/maps/api/place/textsearch/json?&query=taipei+101&key=[Your API Key]"
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Date: Wed, 15 Dec 2021 04:19:33 GMT
Expires: Wed, 15 Dec 2021 04:24:33 GMT
Cache-Control: public, max-age=300
Server: scaffolding on HTTPServer2
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
Server-Timing: gfet4t7; dur=41
Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
Accept-Ranges: none
Vary: Accept-Language,Accept-Encoding
Transfer-Encoding: chunked
|
先不說兩邊得出的 JSON 格式不同
最大的差異是前者的 Header 有 Access-Control-Allow-Origin: *
這段!
這導致 Geocoding API 可以 CORS但 Place API 不可。
補充一下,要在前端使用 Place API 的功能的話,得使用Places Library。
而 Places Library 得同時開啟 Places Library 和 Maps JavaScript API。
沒有留言:
張貼留言