Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import folium | |
| from streamlit_folium import st_folium | |
| from geopy.geocoders import Nominatim | |
| from datetime import datetime | |
| # Initialize Geolocator with English enforced | |
| geolocator = Nominatim(user_agent="Inam65/Simple-map-access", timeout=10) # Remember ALWAYS MATCH YOUR user_agent with NAME OF YOUR APPLICATION | |
| # Streamlit App | |
| st.set_page_config(page_title="World Map Search", layout="wide") | |
| st.title("π Interactive World Map Search (English Only)") | |
| # Session states initialization | |
| if 'latitude' not in st.session_state: | |
| st.session_state['latitude'] = 20.0 | |
| if 'longitude' not in st.session_state: | |
| st.session_state['longitude'] = 0.0 | |
| if 'location_name' not in st.session_state: | |
| st.session_state['location_name'] = "" | |
| # Date input (default today) | |
| default_date = datetime.utcnow().date() | |
| selected_date = st.text_input("Date (YYYY-MM-DD)", value=str(default_date)) | |
| # Layout: 3 columns for inputs | |
| col1, col2, col3 = st.columns(3) | |
| with col1: | |
| latitude_input = st.text_input("Latitude", value=str(st.session_state['latitude'])) | |
| with col2: | |
| longitude_input = st.text_input("Longitude", value=str(st.session_state['longitude'])) | |
| with col3: | |
| location_search = st.text_input("Search Location (English only)", value=st.session_state['location_name']) | |
| # Search button | |
| if st.button("Search"): | |
| with st.spinner("π Searching and updating the location..."): | |
| # First try location search | |
| if location_search.strip() != "": | |
| location = geolocator.geocode(location_search, language="en") | |
| if location: | |
| st.session_state['latitude'] = location.latitude | |
| st.session_state['longitude'] = location.longitude | |
| st.session_state['location_name'] = location.address | |
| st.success(f"β Location found: {location.address}") | |
| st.balloons() | |
| else: | |
| st.error("β Location not found. Please try another keyword.") | |
| # If no location given, fall back to lat/lon | |
| else: | |
| try: | |
| lat = float(latitude_input) | |
| lon = float(longitude_input) | |
| location = geolocator.reverse((lat, lon), language="en") | |
| if location: | |
| st.session_state['location_name'] = location.address | |
| st.success(f"β Coordinates updated to: {location.address}") | |
| st.balloons() | |
| else: | |
| st.warning("β οΈ Valid coordinates but no address found.") | |
| st.session_state['latitude'] = lat | |
| st.session_state['longitude'] = lon | |
| except ValueError: | |
| st.error("β Invalid latitude or longitude format.") | |
| # Map | |
| st.subheader("πΊοΈ Map View:") | |
| # Create Map | |
| m = folium.Map( | |
| location=[st.session_state['latitude'], st.session_state['longitude']], | |
| zoom_start=5, | |
| control_scale=True, | |
| tiles="CartoDB positron" | |
| ) | |
| # Add marker in English popup | |
| folium.Marker( | |
| [st.session_state['latitude'], st.session_state['longitude']], | |
| popup=f"π {st.session_state['location_name'] or 'Selected Location'}", | |
| tooltip="Click for location", | |
| ).add_to(m) | |
| # Display Map | |
| st_data = st_folium(m, width=1000, height=600) | |
| # Update lat/lon if user clicks on the map | |
| if st_data and st_data.get("last_clicked"): | |
| clicked_lat = st_data["last_clicked"]["lat"] | |
| clicked_lon = st_data["last_clicked"]["lng"] | |
| st.session_state['latitude'] = clicked_lat | |
| st.session_state['longitude'] = clicked_lon | |
| try: | |
| location = geolocator.reverse((clicked_lat, clicked_lon), language="en") | |
| if location: | |
| st.session_state['location_name'] = location.address | |
| else: | |
| st.session_state['location_name'] = "" | |
| except: | |
| st.session_state['location_name'] = "" | |
| st.experimental_rerun() | |