引发 ApiClientException(message) geolocation.exceptions.ApiCli


from geolocation.main import GoogleMaps
if __name__=="__main__":
address="hyderabad"
gmap=GoogleMaps(api_key='MY_API_KEY')
locate=gmap.search(location =address)
print(locate.all())
my_location = locate.first()
print(my_location.city)
print(my_location.route)
print(my_location.street_number)
print(my_location.postal_code)
for administrative_area in my_location.administrative_area:
print("%s: %s" % (administrative_area.area_type, administrative_area.name))
print(my_location.country)
print(my_location.country_shortcut)
print(my_location.formatted_address)
print(my_location.lat)
print(my_location.lng)

这是查找您当前位置的代码,但我收到此错误,即

Traceback (most recent call last):
File "/home/student/Desktop/currentlocation.py", line 5, in <module>
location=gmap.search(location=address)
File "/usr/local/lib/python3.6/dist-packages/geolocation/main.py", line 18, in search
return self.geocode.search(location, lat, lng)
File "/usr/local/lib/python3.6/dist-packages/geolocation/geocode/main.py", line 48, in search
data = self.client.get_data(address=address, latitude=latitude, longitude=longitude)
File "/usr/local/lib/python3.6/dist-packages/geolocation/geocode/client.py", line 40, in get_data
return self.send_data(self.API_URL, self.query_parameters)
File "/usr/local/lib/python3.6/dist-packages/geolocation/client.py", line 23, in send_data
self.validator(response)
File "/usr/local/lib/python3.6/dist-packages/geolocation/validators.py", line 28, in __init__
self.validation(response)
File "/usr/local/lib/python3.6/dist-packages/geolocation/validators.py", line 55, in validation
self.validate_google_status(response)
File "/usr/local/lib/python3.6/dist-packages/geolocation/validators.py", line 46, in validate_google_status
raise ApiClientException(message)
geolocation.exceptions.ApiClientException: Request was denied.

帮助我摆脱困境,并建议我如何使用python获取您的当前位置(而不是手动提供位置/地址,即如何自动检测您的位置(

即使我使用该库自己的示例代码,我的有效 API 密钥也会收到此错误; 请注意,这个库已经 4 年没有更新了,所以这可能是这里的问题。

如果您需要使用python进行地理编码,我建议您使用最新且维护的Python Client for Google Maps Web Services。

下面是一个代码示例:

import googlemaps
gmaps = googlemaps.Client(key='YOUR_API_KEY')
geocode_result = gmaps.geocode("hyderabad")
print(geocode_result)

输出:

[{'address_components': [{'long_name': 'Hyderabad', 'short_name': 'Hyderabad', 'types': ['locality', 'political']}, {'long_name': 'Telangana', 'short_name': 'Telangana', 'types': ['administrative_area_level_1', 'political']}, {'long_name': 'India', 'short_name': 'IN', 'types': ['country', 'political']}], 'formatted_address': 'Hyderabad, Telangana, India', 'geometry': {'bounds': {
'northeast': {'lat': 17.6078088, 'lng': 78.6561694}, 'southwest': {'lat': 17.2168886, 'lng': 78.1599217}}, 'location': {'lat': 17.385044, 'lng': 78.486671}, 'location_type': 'APPROXIMATE', 'viewport': {'northeast': {'lat': 17.6078088, 'lng': 78.6561694}, 'southwest': {'lat': 17.2168886, 'lng': 78.1599217}}}, 'place_id': 'ChIJx9Lr6tqZyzsRwvu6koO3k64', 'types': ['locality', 'political']}]

确保您使用的是有效的 API 密钥,即您必须在工程上启用计费和地理编码 API 才能正常工作。请参阅谷歌的入门指南。

希望这有帮助!

相关内容

  • 没有找到相关文章

最新更新