forked from MrHiraiwa/LineBotForGPTPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maps.py
27 lines (25 loc) · 889 Bytes
/
maps.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import os
import googlemaps
from googlemaps.exceptions import ApiError, HTTPError, Timeout, TransportError
google_api_key = os.environ.get('GOOGLE_API_KEY')
gmaps = googlemaps.Client(key=google_api_key)
def get_addresses(latitude, longitude, result_type="sublocality"):
try:
results = gmaps.reverse_geocode(
(latitude, longitude),
result_type=[result_type] if result_type else None,
language='ja'
)
addresses = [result['formatted_address'] for result in results]
return addresses[1]
except ApiError as e:
print(f"API Error: {e}")
except HTTPError as e:
print(f"HTTP Error: {e}")
except Timeout as e:
print(f"Timeout Error: {e}")
except TransportError as e:
print(f"Transport Error: {e}")
except Exception as e:
print(f"General Error: {e}")
return []