geopy 的 GeocoderServiceError

GeocoderServiceError for geopy

当我使用 geopy 根据经度和纬度计算 2 个地址之间的距离时,它可以很好地处理单个数据对。但是当有更多数据时,它总是给我这个错误:

File "/Library/Python/2.7/site-packages/geopy/geocoders/osm.py", line 193, in geocode self._call_geocoder(url, timeout=timeout), exactly_one File "/Library/Python/2.7/site-packages/geopy/geocoders/base.py", line 171, in _call_geocoder raise GeocoderServiceError(message) geopy.exc.GeocoderServiceError: urlopen error [Errno 65] No route to host

你知道我怎样才能避免这个问题吗?

我的代码很简单:(这个输入的数据有很多对数据)

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
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 ''

上面的google地图API是2016年用的,今天(2017-08-30)用的时候发现不能用坐标return定位了.但!我在这里找到了一个非常好的,它现在运行良好,也需要更少的代码行。 https://chrisalbon.com/python/geocoding_and_reverse_geocoding.html