本文整理汇总了Python中googlemaps.GoogleMaps.geocode方法的典型用法代码示例。如果您正苦于以下问题:Python GoogleMaps.geocode方法的具体用法?Python GoogleMaps.geocode怎么用?Python GoogleMaps.geocode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类googlemaps.GoogleMaps
的用法示例。
在下文中一共展示了GoogleMaps.geocode方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_geocode
# 需要导入模块: from googlemaps import GoogleMaps [as 别名]
# 或者: from googlemaps.GoogleMaps import geocode [as 别名]
def test_geocode(self):
addr = u'1600 amphithéatre mountain view ca'
gmaps = GoogleMaps(GMAPS_API_KEY)
results = gmaps.geocode(addr)
first = results[0]
for info in ({'long_name': '1600',
'short_name': '1600',
'types': ['street_number']},
{'long_name': 'Amphitheatre Parkway',
'short_name': 'Amphitheatre Pkwy',
'types': ['route']},
{'long_name': 'Mountain View',
'short_name': 'Mountain View',
'types': ['locality', 'political']},
{'long_name': '94043',
'short_name': '94043',
'types': ['postal_code']},
{'long_name': 'Santa Clara County',
'short_name': 'Santa Clara County',
'types': ['administrative_area_level_2', 'political']},
{'long_name': 'California',
'short_name': 'CA',
'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'],
'1600 Amphitheatre Parkway, Mountain View, CA 94043, USA')
location = first['geometry']['location']
self.assertAlmostEquals(location['lat'], 37.4218281, 3)
self.assertAlmostEquals(location['lng'], -122.0840696, 3)
示例2: coords_from_address
# 需要导入模块: from googlemaps import GoogleMaps [as 别名]
# 或者: from googlemaps.GoogleMaps import geocode [as 别名]
def coords_from_address(address):
gmaps = GoogleMaps(settings.GOOGLE_API_KEY)
try:
result = gmaps.geocode(value)
lng, lat = result['Placemark'][0]['Point']['coordinates'][0:2]
return (lat, lng)
except:
raise Exception("Address can't be parsed by google maps")
示例3: to_address
# 需要导入模块: from googlemaps import GoogleMaps [as 别名]
# 或者: from googlemaps.GoogleMaps import 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
示例4: normalize_street_address
# 需要导入模块: from googlemaps import GoogleMaps [as 别名]
# 或者: from googlemaps.GoogleMaps import geocode [as 别名]
def normalize_street_address(raw_address_string):
gm = GoogleMaps() # Add API key for premium Google Maps service
result = None
try:
data = gm.geocode(raw_address_string)
if data is not None:
result = NormalizedAddress(data)
except KeyError:
result = None
except GoogleMapsError as ex:
if ex.message != GoogleMapsError.G_GEO_UNKNOWN_ADDRESS:
raise ex
return result
示例5: test_geocode
# 需要导入模块: from googlemaps import GoogleMaps [as 别名]
# 或者: from googlemaps.GoogleMaps import geocode [as 别名]
def test_geocode(self):
"""Test googlemaps geocode() and address_to_latlng()"""
addr = '1600 amphitheatre mountain view ca'
gmaps = GoogleMaps(GMAPS_API_KEY)
result = gmaps.geocode(addr)
self.assertEqual(result['Status']['code'], 200)
self.assertEqual(searchkey(result, 'CountryName'), 'USA')
self.assertEqual(searchkey(result, 'PostalCodeNumber'), '94043')
self.assertEqual(searchkey(result, 'ThoroughfareName'), '1600 Amphitheatre Pkwy')
self.assertEqual(searchkey(result, 'LocalityName'), 'Mountain View')
self.assertEqual(searchkey(result, 'AdministrativeAreaName'), 'CA')
self.assertEqual(searchkey(result, 'CountryNameCode'), 'US')
self.assertEqual(searchkey(result, 'address'), '1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA')
lat, lng = searchkey(result, 'coordinates')[1::-1]
self.assertAlmostEquals(lat, 37.422125, 3)
self.assertAlmostEquals(lng, -122.084466, 3)
(lat2, lng2) = gmaps.address_to_latlng(addr)
self.assertAlmostEqual(lat, lat2, 3)
self.assertAlmostEqual(lng2, lng2, 3)