Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,12 +4,12 @@ from streamlit_folium import st_folium
|
|
| 4 |
from geopy.geocoders import Nominatim
|
| 5 |
from datetime import datetime
|
| 6 |
|
| 7 |
-
# Initialize Geolocator
|
| 8 |
-
geolocator = Nominatim(user_agent="
|
| 9 |
|
| 10 |
# Streamlit App
|
| 11 |
st.set_page_config(page_title="World Map Search", layout="wide")
|
| 12 |
-
st.title("🌍 Interactive World Map Search")
|
| 13 |
|
| 14 |
# Session states initialization
|
| 15 |
if 'latitude' not in st.session_state:
|
|
@@ -31,27 +31,28 @@ with col1:
|
|
| 31 |
with col2:
|
| 32 |
longitude = st.text_input("Longitude", value=str(st.session_state['longitude']))
|
| 33 |
with col3:
|
| 34 |
-
location_search = st.text_input("Search Location", value=st.session_state['location_name'])
|
| 35 |
|
| 36 |
# Search button
|
| 37 |
if st.button("Search"):
|
| 38 |
if location_search:
|
| 39 |
try:
|
| 40 |
-
location = geolocator.geocode(location_search)
|
| 41 |
if location:
|
| 42 |
st.session_state['latitude'] = location.latitude
|
| 43 |
st.session_state['longitude'] = location.longitude
|
|
|
|
| 44 |
st.success(f"Location found: {location.address}")
|
| 45 |
else:
|
| 46 |
st.error("Location not found. Try another keyword.")
|
| 47 |
-
except:
|
| 48 |
-
st.error("Error while searching the location
|
| 49 |
else:
|
| 50 |
try:
|
| 51 |
# If no search but lat/lon entered manually
|
| 52 |
lat = float(latitude)
|
| 53 |
lon = float(longitude)
|
| 54 |
-
location = geolocator.reverse((lat, lon))
|
| 55 |
if location:
|
| 56 |
st.session_state['location_name'] = location.address
|
| 57 |
st.success(f"Location updated to: {location.address}")
|
|
@@ -67,6 +68,7 @@ st.subheader("Map View:")
|
|
| 67 |
|
| 68 |
# Create Map
|
| 69 |
m = folium.Map(location=[st.session_state['latitude'], st.session_state['longitude']], zoom_start=5)
|
|
|
|
| 70 |
# Add marker
|
| 71 |
folium.Marker(
|
| 72 |
[st.session_state['latitude'], st.session_state['longitude']],
|
|
@@ -84,7 +86,7 @@ if st_data and st_data.get("last_clicked"):
|
|
| 84 |
st.session_state['latitude'] = clicked_lat
|
| 85 |
st.session_state['longitude'] = clicked_lon
|
| 86 |
try:
|
| 87 |
-
location = geolocator.reverse((clicked_lat, clicked_lon))
|
| 88 |
if location:
|
| 89 |
st.session_state['location_name'] = location.address
|
| 90 |
else:
|
|
|
|
| 4 |
from geopy.geocoders import Nominatim
|
| 5 |
from datetime import datetime
|
| 6 |
|
| 7 |
+
# Initialize Geolocator with English enforced
|
| 8 |
+
geolocator = Nominatim(user_agent="geoapiExercises", timeout=10)
|
| 9 |
|
| 10 |
# Streamlit App
|
| 11 |
st.set_page_config(page_title="World Map Search", layout="wide")
|
| 12 |
+
st.title("🌍 Interactive World Map Search (English Only)")
|
| 13 |
|
| 14 |
# Session states initialization
|
| 15 |
if 'latitude' not in st.session_state:
|
|
|
|
| 31 |
with col2:
|
| 32 |
longitude = st.text_input("Longitude", value=str(st.session_state['longitude']))
|
| 33 |
with col3:
|
| 34 |
+
location_search = st.text_input("Search Location (English only)", value=st.session_state['location_name'])
|
| 35 |
|
| 36 |
# Search button
|
| 37 |
if st.button("Search"):
|
| 38 |
if location_search:
|
| 39 |
try:
|
| 40 |
+
location = geolocator.geocode(location_search, language="en")
|
| 41 |
if location:
|
| 42 |
st.session_state['latitude'] = location.latitude
|
| 43 |
st.session_state['longitude'] = location.longitude
|
| 44 |
+
st.session_state['location_name'] = location.address
|
| 45 |
st.success(f"Location found: {location.address}")
|
| 46 |
else:
|
| 47 |
st.error("Location not found. Try another keyword.")
|
| 48 |
+
except Exception as e:
|
| 49 |
+
st.error(f"Error while searching the location: {e}")
|
| 50 |
else:
|
| 51 |
try:
|
| 52 |
# If no search but lat/lon entered manually
|
| 53 |
lat = float(latitude)
|
| 54 |
lon = float(longitude)
|
| 55 |
+
location = geolocator.reverse((lat, lon), language="en")
|
| 56 |
if location:
|
| 57 |
st.session_state['location_name'] = location.address
|
| 58 |
st.success(f"Location updated to: {location.address}")
|
|
|
|
| 68 |
|
| 69 |
# Create Map
|
| 70 |
m = folium.Map(location=[st.session_state['latitude'], st.session_state['longitude']], zoom_start=5)
|
| 71 |
+
|
| 72 |
# Add marker
|
| 73 |
folium.Marker(
|
| 74 |
[st.session_state['latitude'], st.session_state['longitude']],
|
|
|
|
| 86 |
st.session_state['latitude'] = clicked_lat
|
| 87 |
st.session_state['longitude'] = clicked_lon
|
| 88 |
try:
|
| 89 |
+
location = geolocator.reverse((clicked_lat, clicked_lon), language="en")
|
| 90 |
if location:
|
| 91 |
st.session_state['location_name'] = location.address
|
| 92 |
else:
|