当我使用geopy根据经度和纬度计算2个地址之间的距离时,它可以在单个数据对上正常工作。但是当有更多数据时,它总是给我这个错误:
文件“/Library/Python/2.7/site-packages/geopy/geocoders/osm.py”,第193行,地理编码 self._call_geocoder(url,timeout = timeout),exact_one 在_call_geocoder中输入文件“/Library/Python/2.7/site-packages/geopy/geocoders/base.py”,第171行 raise GeocoderServiceError(message) geopy.exc.GeocoderServiceError:urlopen错误[Errno 65]无主机路由
你知道我怎么能避免这个问题?
我的代码很简单:(输入的数据有很多对数据)
from geopy.geocoders import Nominatim
from geopy.distance import vincenty
def calculate_distance(add1, add2):
geolocator = Nominatim()
location1 = geolocator.geocode(add1)
al1 = (location1.latitude, location1.longitude)
location2 = geolocator.geocode(add2)
al2 = (location2.latitude, location2.longitude)
distce = vincenty(al1, al2).miles
return distce
答案 0 :(得分:2)
def get_level1_locations(lat_lng):
elems = lat_lng.split(',')
url = "http://maps.googleapis.com/maps/api/geocode/json?"
url += "latlng=%s,%s&sensor=false" % (float(elems[0]), float(elems[1]))
v = urlopen(url).read()
j = json.loads(v)
if len(j['results'])>0:
components = j['results'][0]['address_components']
location_list=[]
for c in components:
if "locality" in c['types']:
location_list.append(c['long_name'])
if "administrative_area_level_1" in c['types']:
location_list.append(c['long_name'])
if "country" in c['types']:
location_list.append(c['long_name'])
location = ', '.join(filter(None, location_list))
return location
return ''
答案 1 :(得分:0)
上面的Google地图API工作于2016年,今天(2017-08-30),当我尝试使用它时,它不再能够通过坐标返回位置。但!我在这里找到了一个非常好的,它现在运行良好,也需要更少的代码行。 https://chrisalbon.com/python/geocoding_and_reverse_geocoding.html