![](https://fineproxy.org/wp-content/uploads/2024/05/129pAhlOiVtYjWHKsFSr74qa3xwyoesbE3w1Mfgp-2x.jpg)
Google Maps Places API 是一款功能强大的工具,可让开发人员访问有关世界各地地点的详细信息。在本教程中,我们将指导您完成使用 Python 中的新 Google Maps Places API 的过程,从设置您的 Google Cloud 项目到发出 API 请求和处理响应。
什么是 Google Maps Places API?
Google Maps 有两个版本的 Places API:
- 旧版 API – 允许使用简单的 API 密钥进行连接。
- 下一代 API – 需要 OAuth 2.0 进行身份验证并提供更多高级功能。
设置您的Google Cloud项目
- 导航到 Google Cloud Console: 去 Google 云端控制台.
- 创建项目:如果您没有项目,请点击下拉菜单并选择“新项目”来创建一个。
- 启用 API 和服务:转到 API 和服务 > 仪表板,然后启用地点 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 Maps 的世界吧!
评论 (0)
这里还没有评论,你可以成为第一个评论者!