在Python中使用Google API获取坐标(经度和纬度)

时间:2018-10-23 08:50:55

标签: python pandas google-maps

如果我的地址数据框如下:

id                Address                        Latitude       Longitude 
01   Via Benedetto Croce 112, Rome, Italy          
02   Via Aristide Leonori 46, Rome, Italy
03   Viale Marconi 197, Rome, Italy

如何在Python中使用Google API获取坐标(经度和纬度)?谢谢。

id                Address                        Latitude       Longitude 
01   Via Benedetto Croce 112, Rome, Italy       41.8423561    12.48535240000001
02   Via Aristide Leonori 46, Rome, Italy       41.8487061    12.488554900000054       
03   Viale Marconi 197, Rome, Italy             41.8567032    12.465537500002210

这是我从Google Maps API Web Services的Python客户端库中找到的内容,但失败了:提高googlemaps.exceptions.Timeout()

https://github.com/googlemaps/google-maps-services-python

import googlemaps
from datetime import datetime

gmaps = googlemaps.Client(key='googlemap ak')
# Geocoding an address
geocode_result = gmaps.geocode('Via Benedetto Croce 112, Rome, Italy')
print(geocode_result)

1 个答案:

答案 0 :(得分:1)

尝试一下。它使用Nominatim。

import pandas as pd
from geopy.geocoders import Nominatim
geolocator = Nominatim()
data = [
        {'Id':'01', 'Address': "Via Benedetto Croce 112, Rome, Italy", 'Latitude': None, 'Longitude' :None},
        {'Id':'02', 'Address': "Via Aristide Leonori 46, Rome, Italy", 'Latitude': None, 'Longitude' :None},
        {'Id':'03', 'Address': "Viale Marconi 197, Rome, Italy", 'Latitude': None, 'Longitude' :None}
       ]
df = pd.DataFrame(data)
df['city_coord']  = df['Address'].apply(geolocator.geocode)
df['Latitude'] = df['city_coord'].apply(lambda x: (x.latitude))
df['Longitude'] = df['city_coord'].apply(lambda x: (x.longitude))
print(df[['Address',  'Id',   'Latitude','Longitude']])

enter image description here