如何在 Google 个地方 API 中获取多个地名?

How to get multiple places names in Google Places API?

在我的Django项目中,我让用户标记他们最近在哪个城市。我为此使用 Google Places API - 使用地图自动完成。

我有多个用例,我无法弄清楚如何使用 Google Places API T&C,这是其中之一:

有一个用户过滤器。每个用户都选择了他们上次访问的地方(所以我可以存储place_id)。

当用户筛选出 100 个用户时,他们会在结果中按卡片一一显示。该卡片应包含用户上次访问的地点。

所以我必须出示 100 张卡片,其中包含 100 个地点(城市)。

如果我只有 place_id 怎么办?我是否应该向 Google Places API 发送 100 个详细请求以获取每个城市的名称?

def user_filter(request):
    filtered_users = ....
    for user in filtered_users:
        api_result = request.get('https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJN1t_tDeuEmsRUsoyG83frY4&key=MY_API_KEY')
        recent_city_name = parse_from_result(api_result)
    ....
    return render(request,....)

虽然这在理论上应该可行,但这是一个很大的矫枉过正 - 耗时和 API 请求消耗。有更好的方法吗?

我没有找到任何批量请求选项。

确实Google地图API不支持批量操作,发送数百个请求肯定很耗时。

我相信您可以使用服务条款第 10.5 (d) 段

No caching or storage. You will not pre-fetch, cache, index, or store any Content to be used outside the Service, except that you may store limited amounts of Content solely for the purpose of improving the performance of your Maps API Implementation due to network latency (and not for the purpose of preventing Google from accurately tracking usage), and only if such storage:

  • is temporary (and in no event more than 30 calendar days);
  • is secure;
  • does not manipulate or aggregate any part of the Content or Service; and
  • does not modify attribution in any way.

https://developers.google.com/maps/terms?#section_10_5

我相信,您符合我用粗体标记的文字。您的目的是提高应用程序的性能,因此您可以临时存储一个关联地点 ID 和城市名称的哈希映射,并每 30 天刷新一次临时存储以符合服务条款。

希望对您有所帮助!