Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving marker to click location #122

Open
adolmajian opened this issue Apr 1, 2023 · 2 comments
Open

Moving marker to click location #122

adolmajian opened this issue Apr 1, 2023 · 2 comments

Comments

@adolmajian
Copy link

adolmajian commented Apr 1, 2023

I'm trying to move a single marker on the map to where the user clicks on the map but I'm struggling. With every click (st_folium) the marker is moving to the previous click location instead of the current click location. What I am doing wrong here?

# State variables
if 'center' not in st.session_state:
    st.session_state.center = [45.503032, -73.566424]

if 'zoom' not in st.session_state:
    st.session_state.zoom = 15

if 'location' not in st.session_state:
    st.session_state.location = folium.Marker(st.session_state.center)

# Map creation
m = folium.Map(location=st.session_state.center, zoom_start=st.session_state.zoom)
fg = folium.FeatureGroup(name="Markers")
fg.add_child(st.session_state.location)

# When the user interacts with the map
map_state_change = st_folium(
      m,
      feature_group_to_add=fg,
      height=400,
      width='100%',
      returned_objects=['last_clicked', 'zoom', 'bounds', 'center'],
)

# If the interaction includes a click
if map_state_change['last_clicked']:
    loc = map_state_change['last_clicked']
    st.session_state.location = folium.Marker([loc['lat'], loc['lng']])
@SirVectrex
Copy link

I am currently struggling with the same issue.

Adding a button, that forces a zoom change is a work-around.

Were you able to find a better solution? @adolmajian

@adolmajian
Copy link
Author

What I ended up doing is not relying on a click at all. I added a crosshair as text in my Streamlit app and some hacky markdown to position the crosshair in the center of the map. You can see the app and code at https://isochrone.streamlit.app/.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants