使用相同的内容请求Google Map API并获得不同的结果

时间:2018-11-16 16:29:39

标签: python pandas google-maps google-maps-api-3

这几天我遇到了一个非常奇怪的问题,无法解决。请帮助! 我想使用google map api获取地址的经度和纬度。这是我对请求api的函数的定义:

def get_coordinates(df):
if pd.notnull(df['geocode']):
    address=df['geocode']
    response = requests.get("https://maps.googleapis.com/maps/api/geocode/json?address=" 
                        + address+"&key=my-key")
    json_response = response.json()

    if len(json_response['results'])==0:
        return 'None'
    else:
        coordinates=json_response['results'][0]['geometry']['location']
        latitude=coordinates['lat']
        longitude=coordinates['lng']

        l_l=[latitude, longitude]
        return l_l
else:
    return 'None'

我将地址存储在数据框中。
enter image description here
然后我可以使用df.apply为每个地址请求api:

test2['coor1'] = test2.apply(get_coordinates,axis = 1)

但是然后真正的奇怪的事情是:我知道这些地址大多数应该在巴西,但是当我使用散点图绘制它们时,我注意到很多要点不在巴西。所以我想api请求出了点问题,然后我再次运行apply,这一次,某个地址的位置与上次完全不同。我不知道为什么。我想知道以前是否有人遇到过同样的问题。非常感谢!

0 个答案:

没有答案