File size: 1,252 Bytes
c44d66d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from bokeh.models.widgets import Button
from bokeh.models import CustomJS
from streamlit_bokeh_events import streamlit_bokeh_events
from geopy.geocoders import Nominatim

loc_button = Button(label="Get Location")
loc_button.js_on_event("button_click", CustomJS(code="""
    navigator.geolocation.getCurrentPosition(
        (loc) => {
            document.dispatchEvent(new CustomEvent("GET_LOCATION", {detail: {lat: loc.coords.latitude, lon: loc.coords.longitude}}))
        }
    )
    """))
result = streamlit_bokeh_events(
    loc_button,
    events="GET_LOCATION",
    key="get_location",
    refresh_on_update=False,
    override_height=75,
    debounce_time=0)

if result:
    if "GET_LOCATION" in result:
        st.write(result.get("GET_LOCATION"))
        # print(result.get("GET_LOCATION")["lat"])       
        lat = str(result.get("GET_LOCATION")["lat"])
        lon = str(result.get("GET_LOCATION")["lon"])

        # Get address from given coordinate
        geolocator = Nominatim(user_agent="BAAM")
        location = geolocator.reverse(lat + "," + lon)
        address = location.raw['address']
        st.write(str(location))
        st.write(address)
        print(address)
        print(type(str(location)))