File size: 3,877 Bytes
e6d0365
 
 
 
 
 
dd9b100
7031966
e6d0365
 
 
dd9b100
e6d0365
 
 
 
 
 
 
 
 
 
 
 
 
74b49cd
e6d0365
 
 
74b49cd
e6d0365
74b49cd
e6d0365
dd9b100
e6d0365
 
 
74b49cd
 
 
dd9b100
e6d0365
 
 
dd9b100
65d657f
74b49cd
e6d0365
74b49cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6d0365
 
74b49cd
e6d0365
 
74b49cd
 
 
 
e63e435
74b49cd
dd9b100
74b49cd
e6d0365
 
74b49cd
e6d0365
 
 
 
 
 
 
 
 
 
 
 
 
dd9b100
e6d0365
 
 
 
 
 
74b49cd
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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()