本文整理汇总了Python中googlemaps.GoogleMaps.reverse_geocode方法的典型用法代码示例。如果您正苦于以下问题:Python GoogleMaps.reverse_geocode方法的具体用法?Python GoogleMaps.reverse_geocode怎么用?Python GoogleMaps.reverse_geocode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类googlemaps.GoogleMaps
的用法示例。
在下文中一共展示了GoogleMaps.reverse_geocode方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_reverse_geocode
# 需要导入模块: from googlemaps import GoogleMaps [as 别名]
# 或者: from googlemaps.GoogleMaps import reverse_geocode [as 别名]
def test_reverse_geocode(self):
lat, lng = 40.714224, -73.961452
gmaps = GoogleMaps(GMAPS_API_KEY)
results = gmaps.reverse_geocode(lat, lng)
first = results[0]
for info in ({'long_name': '285',
'short_name': '285',
'types': ['street_number']},
{'long_name': 'Bedford Ave',
'short_name': 'Bedford Ave',
'types': ['route']},
{'long_name': 'New York',
'short_name': 'New York',
'types': ['locality', 'political']},
{'long_name': '11211',
'short_name': '11211',
'types': ['postal_code']},
{'long_name': 'New York',
'short_name': 'NY',
'types': ['administrative_area_level_1', 'political']},
{'long_name': 'United States',
'short_name': 'US',
'types': ['country', 'political']}):
self.assertIn(info, first['address_components'])
self.assertEqual(first['formatted_address'],
'285 Bedford Ave, Brooklyn, NY 11211, USA')
location = first['geometry']['location']
self.assertAlmostEquals(location['lat'], lat, 3)
self.assertAlmostEquals(location['lng'], lng, 3)
示例2: to_address
# 需要导入模块: from googlemaps import GoogleMaps [as 别名]
# 或者: from googlemaps.GoogleMaps import reverse_geocode [as 别名]
def to_address(value, google_api_key, min_accuracy=1):
if value is None:
return None
gmaps = GoogleMaps(google_api_key)
if isinstance(value, (list, tuple)) and len(value) >= 2:
if isinstance(value[0], (int, float)) and isinstance(value[1], (int, float)):
result = gmaps.reverse_geocode(value[0], value[1])
else:
result = gmaps.geocode(value[0] + ' near ' + value[1])
else:
result = gmaps.geocode(value)
address = result['Placemark'][0]['address']
details = result['Placemark'][0]['AddressDetails']
accuracy = details['Accuracy']
# Raise an error if the accuracy is insufficient.
if accuracy < min_accuracy:
raise GoogleMapsError(602) # unknown address
country = containers.find(details, 'CountryName', '')
country_code = containers.find(details, 'CountryNameCode', '')
state = containers.find(details, 'AdministrativeAreaName', '')
state_code = containers.find(details, 'AdministrativeAreaNameCode', '')
locality = containers.find(details, 'LocalityName', '')
postal_code = containers.find(details, 'PostalCodeNumber', '')
street_address = containers.find(details, 'ThoroughfareName', '')
try:
lng, lat = result['Placemark'][0]['Point']['coordinates'][0:2]
except:
lng, lat = None, None
# Run through some common fixups.
address_dict = dict(
country=country,
country_code=country_code,
state=state,
state_code=state_code,
locality=locality,
street_address=street_address,
postal_code=postal_code,
formatted=address,
latitude=lat,
longitude=lng,
)
address_fixups(address_dict)
return address_dict
示例3: test_reverse_geocode
# 需要导入模块: from googlemaps import GoogleMaps [as 别名]
# 或者: from googlemaps.GoogleMaps import reverse_geocode [as 别名]
def test_reverse_geocode(self):
"""Test googlemaps reverse_geocode() and latlng_to_address()"""
lat, lng = 40.714224, -73.961452
gmaps = GoogleMaps(GMAPS_API_KEY)
result = gmaps.reverse_geocode(lat, lng)
self.assertEqual(result['Status']['code'], 200)
result = result['Placemark'][0]
self.assertEqual(searchkey(result, 'CountryName'), 'USA')
self.assertEqual(searchkey(result, 'PostalCodeNumber'), '11211')
self.assertEqual(searchkey(result, 'ThoroughfareName'), '277 Bedford Ave')
self.assertEqual(searchkey(result, 'LocalityName'), 'Brooklyn')
self.assertEqual(searchkey(result, 'AdministrativeAreaName'), 'NY')
self.assertEqual(searchkey(result, 'CountryNameCode'), 'US')
addr = searchkey(result, 'address')
self.assertEqual(addr, '277 Bedford Ave, Brooklyn, NY 11211, USA')
lat2, lng2 = searchkey(result, 'coordinates')[1::-1]
self.assertAlmostEquals(lat, lat2, 3)
self.assertAlmostEquals(lng, lng2, 3)
addr2 = gmaps.latlng_to_address(lat, lng)
self.assertEqual(addr, addr2)
示例4: test_reverse_geocode
# 需要导入模块: from googlemaps import GoogleMaps [as 别名]
# 或者: from googlemaps.GoogleMaps import reverse_geocode [as 别名]
def test_reverse_geocode(self):
"""Test googlemaps reverse_geocode() and latlng_to_address()"""
lat, lng = 40.714224, -73.961452
gmaps = GoogleMaps(GMAPS_API_KEY)
result = gmaps.reverse_geocode(lat, lng)
self.assertEqual(result['status'], 'OK')
result = result['results'][0]
# self.assertEqual(searchkey(result, 'CountryName'), 'USA')
# self.assertEqual(searchkey(result, 'PostalCodeNumber'), '11211')
# self.assertEqual(searchkey(result, 'ThoroughfareName'), '277 Bedford Ave')
# self.assertEqual(searchkey(result, 'LocalityName'), 'Brooklyn')
# self.assertEqual(searchkey(result, 'AdministrativeAreaName'), 'NY')
# self.assertEqual(searchkey(result, 'CountryNameCode'), 'US')
addr = searchkey(result, 'formatted_address')
self.assertEqual(addr, '285 Bedford Avenue, Brooklyn, NY 11211, USA')
loc = searchkey(result, 'location')
lat2, lng2 = loc['lat'], loc['lng']
self.assertAlmostEquals(lat, lat2, 3)
self.assertAlmostEquals(lng, lng2, 3)
addr2 = gmaps.latlng_to_address(lat, lng)
self.assertEqual(addr, addr2)