当前位置: 首页>>代码示例>>Python>>正文


Python GoogleMaps.reverse_geocode方法代码示例

本文整理汇总了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)
开发者ID:Polyconseil,项目名称:py-googlemaps,代码行数:31,代码来源:test_googlemaps.py

示例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
开发者ID:furious-luke,项目名称:substar,代码行数:45,代码来源:conv.py

示例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)
开发者ID:coxmediagroup,项目名称:py-googlemaps,代码行数:24,代码来源:test_googlemaps.py

示例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)
开发者ID:georgefs,项目名称:googlemaps-1.0.2b,代码行数:25,代码来源:test_googlemaps.py


注:本文中的googlemaps.GoogleMaps.reverse_geocode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。