-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_coordinates.py
More file actions
36 lines (28 loc) · 1.12 KB
/
get_coordinates.py
File metadata and controls
36 lines (28 loc) · 1.12 KB
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
28
29
30
31
32
33
34
35
36
from dotenv import load_dotenv
import os
import googlemaps
load_dotenv()
def get_coordinates(location):
api_key = os.getenv("GOOGLE_MAPS_API_KEY")
if not api_key:
raise ValueError("API key not found in environment variables.")
gmaps = googlemaps.Client(key=api_key)
geocode_result = gmaps.geocode(location)
if geocode_result:
formatted_address = geocode_result[0]['formatted_address']
coordinates = geocode_result[0]['geometry']['location']
return {
"complete_address": formatted_address,
"lat": coordinates['lat'],
"lng": coordinates['lng']
}
else:
raise ValueError("No results found for the given location.")
if __name__ == "__main__":
location = input("Enter Location: ")
try:
coordinates = get_coordinates(location)
result = {"complete address": coordinates["complete_address"], "latitude": coordinates['lat'], "longitude": coordinates['lng']}
print(f"Coordinates for {location}: {coordinates}")
except Exception as e:
print(f"Error fetching coordinates: {e}")