Google Maps Places API は、開発者が世界中の場所に関する詳細な情報にアクセスできるようにする強力なツールです。このチュートリアルでは、Google Cloud プロジェクトの設定から API リクエストの作成、レスポンスの処理まで、Python で新しい Google Maps Places API を使用するプロセスについて説明します。
Google Maps Places API とは何ですか?
Google マップには Places API の 2 つのバージョンがあります。
- レガシーAPI – シンプルな API キーで接続できます。
- 次世代API – 認証には OAuth 2.0 が必要であり、より高度な機能を提供します。
Google Cloud プロジェクトの設定
- Google Cloud Consoleに移動する: へ移動 Google Cloud コンソール.
- プロジェクトを作成する: プロジェクトがない場合は、ドロップダウン メニューをクリックして「新しいプロジェクト」を選択してプロジェクトを作成します。
- APIとサービスを有効にする: 「API とサービス」>「ダッシュボード」に移動し、Places API を有効にします。
- OAuth 2.0 を設定する: 「API とサービス」>「OAuth 同意画面」に移動し、同意画面を設定して、OAuth 2.0 認証情報を作成します。
OAuth 2.0 による認証
- OAuth クライアント ID を作成するアプリケーションの種類として「デスクトップ アプリ」を選択し、クライアント シークレット ファイルをダウンロードします。
- 必要なライブラリをインストールする: pip を使用して、Google Python クライアント ライブラリと Python dotenv パッケージをインストールします。
pip install google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client python-dotenv
APIリクエストの作成
API クライアントの初期化
Pythonスクリプトを作成する(例: maps_textsearch.py
) をダウンロードし、ダウンロードした資格情報を使用して API クライアントを初期化します。
from google.oauth2 import service_account
from googleapiclient.discovery import build
import json
def create_service(client_secret_file, api_name, api_version, scopes):
credentials = service_account.Credentials.from_service_account_file(client_secret_file, scopes=scopes)
return build(api_name, api_version, credentials=credentials)
CLIENT_SECRET_FILE = 'path/to/client_secret.json'
API_SERVICE_NAME = 'places'
API_VERSION = 'v1'
SCOPES = ['https://www.googleapis.com/auth/maps']
service = create_service(CLIENT_SECRET_FILE, API_SERVICE_NAME, API_VERSION, SCOPES)
例: テキスト検索
検索パラメータを定義する: 特定の場所を検索するためのクエリを作成します。
query = 'ramen places in New York'
response = service.places().textSearch(query=query).execute()
print(json.dumps(response, indent=4))
応答を処理する: 関連情報を抽出して表示します。
for place in response['results']:
print(place['name'], place['formatted_address'])
例: 場所の詳細
場所の詳細を取得する: 場所 ID を使用して特定の場所の詳細情報を取得します。
place_id = 'ChIJN1t_tDeuEmsRUsoyG83frY4'
response = service.places().get(place_id=place_id).execute()
print(json.dumps(response, indent=4))
詳細を表示: レビュー、営業時間などの詳細を抽出して印刷します。
print('Name:', response['name'])
print('Address:', response['formatted_address'])
print('Phone:', response['formatted_phone_number'])
価格と割り当て
Google は、すべてのマップ製品に対して毎月 $200 の無料クレジットを提供しています。予期しない請求を避けるために、使用状況を監視してください。
結論
これらの手順に従うことで、Google Maps Places API を Python アプリケーションに正常に統合し、豊富な場所関連データを提供してユーザー エクスペリエンスを向上できます。
表: 要点
ステップ | 説明 |
---|---|
プロジェクトのセットアップ | Google Cloud Console でプロジェクトを作成し、Places API を有効にして、OAuth 2.0 認証情報を設定します。 |
認証 | OAuth 2.0 を使用して API リクエストを認証します。 |
APIリクエスト | Python クライアント ライブラリを使用して API リクエストを行います。 |
レスポンス処理 | API 応答から関連情報を抽出して表示します。 |
価格 | Google マップが提供する $200 の無料クレジットを活用します。 |
コード例のスニペット
from google.oauth2 import service_account
from googleapiclient.discovery import build
def create_service(client_secret_file, api_name, api_version, scopes):
credentials = service_account.Credentials.from_service_account_file(client_secret_file, scopes=scopes)
return build(api_name, api_version, credentials=credentials)
CLIENT_SECRET_FILE = 'path/to/client_secret.json'
API_SERVICE_NAME = 'places'
API_VERSION = 'v1'
SCOPES = ['https://www.googleapis.com/auth/maps']
service = create_service(CLIENT_SECRET_FILE, API_SERVICE_NAME, API_VERSION, SCOPES)
query = 'ramen places in New York'
response = service.places().textSearch(query=query).execute()
print(json.dumps(response, indent=4))
for place in response['results']:
print(place['name'], place['formatted_address'])
このガイドに従うことで、Google Maps Places API を簡単に使い始めることができ、豊富な位置情報ベースのデータをアプリケーションで利用できるようになります。Google マップの世界をお楽しみください。
コメント (0)
まだコメントはありません。あなたが最初のコメントを投稿できます!