はじめに
ここでは、Google Maps PlatformのText Search APIを実際に使ってみよう思います
事前準備
- Google Cloud Platformで「Places API」を有効化
-
APIキーの取得
管理コンソールでAPIキーを発行してください
必要に応じてアクセス制限を設定してください
リクエスト方法
HTTPリクエストの形式
REST(POSTリクエスト)
エンドポイント
https://places.googleapis.com/v1/places:searchText
使用例 検索ワード:東京駅 寿司
curl -X POST -d '{
"textQuery": "東京駅 寿司"
}' \
-H 'Content-Type: application/json' \
-H 'X-Goog-Api-Key: {API_KEY}' \
-H 'X-Goog-FieldMask: places.id,places.displayName,places.formattedAddress,places.location' \
'https://places.googleapis.com/v1/places:searchText'
レスポンス例
JSON形式で返却されます
サンプルレスポンス
{
"places": [
{
"id": "ChIJ1eQxcKRhGGARZrF_R7eIYF0",
"formattedAddress": "3-chōme-24-7 Ōmorinishi, Ota City, Tokyo 143-0015, Japan",
"location": {
"latitude": 35.5720807,
"longitude": 139.7317798
},
"displayName": {
"text": "KEIKYU_FREE_Wi-Fi (KK09)",
"languageCode": "ja"
}
}
]
}
他も指定してみる
curl -X POST -d '{
"textQuery": "東京駅周辺のおすすめのレストラン",
"pageSize": 2,
"languageCode": "ja"
}' \
-H 'Content-Type: application/json' \
-H 'X-Goog-Api-Key: {API_KEY}' \
-H 'X-Goog-FieldMask: places.displayName,places.formattedAddress' \
'https://places.googleapis.com/v1/places:searchText'
{
"places": [
{
"formattedAddress": "日本、〒143-0016 東京都大田区大森北1丁目11−5 共和七番館地下1階",
"displayName": {
"text": "WWG",
"languageCode": "ja"
}
},
{
"formattedAddress": "日本、〒150-0002 東京都渋谷区渋谷2丁目9−9 SANWA青山Bldg 5階",
"displayName": {
"text": "株式会社g-wic",
"languageCode": "ja"
}
}
]
}
リファレンスには以下内容の記載がありますが、
textQuery
検索するテキスト文字列(「レストラン」、「123 番通り」、「サンフランシスコのおすすめスポット」など)。API はこの文字列と一致する候補を、関連性の高い順に並べて結果として返します。
試した "東京駅周辺のおすすめのレストラン" のレスポンス結果は
イマイチな気もします・・・
場所が明確であれば パラメータlocationBiasなどで場所を明示的に指定したほうが良いでしょう
curl -X POST -d '{
"textQuery": "レストラン",
"pageSize": 2,
"locationBias": {
"circle": {
"center": {
"latitude": 35.681364390920265,
"longitude": 139.7671035631151
},
"radius": 500.0
}
},
"languageCode": "ja"
}' \
-H 'Content-Type: application/json' \
-H 'X-Goog-Api-Key: {API_KEY}' \
-H 'X-Goog-FieldMask: places.id,places.displayName,places.formattedAddress,places.location,places.websiteUri' \
'https://places.googleapis.com/v1/places:searchText'
{
"places": [
{
"id": "ChIJAQAsR--LGGAR_AmB8WMDy88",
"formattedAddress": "日本、〒104-0061 東京都中央区銀座6丁目10−1",
"location": {
"latitude": 35.6697688,
"longitude": 139.76417619999998
},
"websiteUri": "https://ginza6.tokyo/",
"displayName": {
"text": "GINZA SIX",
"languageCode": "ja"
}
},
{
"id": "ChIJ__8Lhf2LGGARYj6XJNBik14",
"formattedAddress": "日本、〒103-8265 東京都中央区日本橋2丁目4−1",
"location": {
"latitude": 35.6808012,
"longitude": 139.7735107
},
"websiteUri": "https://www.takashimaya.co.jp/nihombashi/index.html",
"displayName": {
"text": "日本橋高島屋S.C.",
"languageCode": "ja"
}
}
]
}
確かに指定した座標周辺の結果となっていますが、
レストランの入っている建物が返ってきているように思います
textQueryの使い方はもう少し工夫が必要なのかもしれません
まとめ
Text Search APIは、
キーワードや住所などのテキスト入力からGoogleマップの膨大な施設情報を柔軟に検索できる強力なAPIである。
ただ、非常に柔軟な検索が可能である一方、Googleマップのデータベースや検索アルゴリズムの仕様、あるいはリクエスト時のパラメータ設定によって、得られる情報に限界があることを実感