-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeo_coordinate with map.py
More file actions
57 lines (46 loc) · 1.91 KB
/
geo_coordinate with map.py
File metadata and controls
57 lines (46 loc) · 1.91 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import tkinter as tk
from tkinter import ttk
from geopy.geocoders import Nominatim
import folium
from folium import plugins
class GeocodingTool:
def __init__(self, root):
self.root = root
self.root.title("Geocoding Tool")
self.geolocator = Nominatim(user_agent="geocoding_tool")
self.create_widgets()
def geocode_address(self):
address = self.address_entry.get()
try:
location = self.geolocator.geocode(address)
if location:
self.result_label.config(text=f"Latitude: {location.latitude}, Longitude: {location.longitude}")
self.display_map(location.latitude, location.longitude)
else:
self.result_label.config(text="Location not found.")
except Exception as e:
self.result_label.config(text=f"Error: {str(e)}")
def display_map(self, latitude, longitude):
m = folium.Map(location=[latitude, longitude], zoom_start=15)
folium.Marker([latitude, longitude], popup="Location").add_to(m)
m.save("map.html")
# Open the map in the default web browser
import webbrowser
webbrowser.open("map.html")
def create_widgets(self):
# Address Entry
address_label = tk.Label(self.root, text="Enter Address:")
address_label.grid(row=0, column=0, padx=10, pady=10)
self.address_entry = ttk.Entry(self.root)
self.address_entry.grid(row=0, column=1, padx=10, pady=10)
# Geocode Button
geocode_button = ttk.Button(self.root, text="Geocode", command=self.geocode_address)
geocode_button.grid(row=1, column=0, columnspan=2, pady=10)
# Result Label
self.result_label = tk.Label(self.root, text="")
self.result_label.grid(row=2, column=0, columnspan=2, pady=10)
# Example usage
if __name__ == "__main__":
root = tk.Tk()
app = GeocodingTool(root)
root.mainloop()