2016-02-06 58 views
4

當我使用geopy根據它們的經度和緯度計算2個地址之間的距離時,它可以在單個數據對上正常工作。但是,當有更多的數據,它總是給我這個錯誤:geocoderServiceError for geopy

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 
+0

看起來像網絡錯誤,而不是API問題。 – user1157751

+0

它每次都發生,我的網絡正常工作.... @ user1157751 –

+2

它也發生在我身上。由於請求太多,我傾向於認爲我被列入黑名單。 – Hazam

回答

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 '' 
相關問題