google-maps - 当我没有超过每日免费请求数时,为什么我总是收到 MAX_ELEMENTS_EXCEEDED 错误?

标签 google-maps google-maps-api-3 google-distancematrix-api

所以我开发了一个代码,我在其中读取了一个文件,其中包含一些位置(更准确地说是 15 个),其中第一个是车站,其他 14 个位置是公共(public)汽车需要通过以收集患者的地方。为此,我使用 Google Maps API key 来收集真实距离,最后将它们写入 .txt 文件。

import pandas as pd
import googlemaps
from itertools import tee
import numpy as np

#Read CSV file into data frame named 'df'
#change seperator (sep e.g. ',') type if necessary
df = pd.read_csv("D:/Utilizadores/Documents/FEUP/2018-2019/1º Semestre/PDI/Relatório/locais2.txt", sep='\\t',
                       engine='python', skiprows=[0], names=["address", "latitude", "longitude"])

lat = np.expand_dims(np.array(df["latitude"]), 1)
lon = np.expand_dims(np.array(df["longitude"]), 1)
coordinates = np.concatenate((lat, lon), axis=1)

coordinates = list(coordinates)
coordinates = [list(coor) for coor in coordinates]


#Perform request to use the Google Maps API web service
#API_key = 'AIzaSyCi8DDz_CCiVwW2JtvT6i-XpJYiEwxFryI'
API_key = 'AIzaSyCpumDcRbbteV64xlGOUQ5_Bah8Ja5gdJ4'
gmaps = googlemaps.Client(key=API_key)

result = gmaps.distance_matrix(coordinates, coordinates, mode='driving')["rows"]
distance_matrix = np.zeros((len(result), len(result)))

for i in range(len(result)):
    for j in range(len(result)):
        distance_matrix[i, j] = result[i]["elements"][j]["distance"]["value"]

np.savetxt("D:/Utilizadores/Documents/FEUP/2018-2019/1º Semestre/PDI/Relatório/locais_output.txt", distance_matrix, delimiter=",", fmt='%d')
print(distance_matrix)

我想要的距离是从一个地方到每个地方,所以我想要的结果是一个 15x15 的矩阵,其中对角线用 0 填充。但它一直打印这个错误:

"googlemaps.exceptions.ApiError: MAX_ELEMENTS_EXCEEDED".

唯一不出错的方法是限制读取 10 个位置的文件,包括 depot:

result = gmaps.distance_matrix(coordinates[0:10], coordinates[0:10], mode='driving')["rows"]

这是为什么呢?有人吗?

最佳答案

来自 the documentation

MAX_ELEMENTS_EXCEEDED indicates that the product of origins and destinations exceeds the per-query limit.

来自 "usage and billing" :

Each query sent to the Distance Matrix API generates elements, where the number of origins times the number of destinations equals the number of elements.

Other Usage Limits
While you are no longer limited to a maximum number of elements per day (EPD), the following usage limits are still in place for the Distance Matrix API:

Maximum of 25 origins or 25 destinations per request.
Maximum 100 elements per server-side request.
Maximum 100 elements per client-side request.
1000 elements per second (EPS), calculated as the sum of client-side and server-side queries.

15 x 15 = 225 大于允许的最大值 (100)

您不想要或不需要某些响应是无关紧要的。

关于google-maps - 当我没有超过每日免费请求数时,为什么我总是收到 MAX_ELEMENTS_EXCEEDED 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56405576/

相关文章:

api - 应用程序运行时 Flutter Google Map API 问题

api - 从当前位置到标记的 Google map API 路线

android - 添加 ML Kit 后无法解析 : com. google.android.gms :play-services:15. 0.1

python - 谷歌距离矩阵 API : How to specify origin-destination pairs?

api - 如何使用 Google Maps GeoCoding API 获取城市内的所有地标?

android - Lua Service2Media 移动端

javascript - 获取道路的几何形状(线条/绘图)

google-maps - 与 Google Maps 相比,Google Maps Distance Matrix API 返回不同的输出

google-maps-api-3 - 谷歌距离矩阵 API

google-maps - 是否必须在 2018 年 6 月 11 日之前通过信用卡信息启用计费帐户才能继续访问 Google Maps API?