Add marker after clicking on map

Hi community, a suggestion, there is a basic tutorial missing, how to add a marker to a click on a map event. I have been struggling with this and I cannot find it anywhere. I am still struggling a lot with session state, and I know I am not the only one as I found a person with the exact same problem. Here is what I managed to do, but I have to click two times on the map to get the marker there:

the idea: display a marker on the map once the user clicks on the map

expected behaviour: marker appears where the user clicked, if the user clicks somewhere else, the marker appears over the new click

actual behaviour: user clicks once on the map and nothing happens, then clicks again and marker appears over the previous click, not the last

    if 'center' not in st.session_state: = [46.903354, 1.888334]
    # Initialize session state to store clicked points
    if 'location' not in st.session_state:
        st.session_state.location = folium.Marker(

    # Create a Folium map
    m = folium.Map(location=[46.903354, 1.888334], zoom_start=6)
    fg = folium.FeatureGroup(name="Markers")

    # When the user interacts with the map
    map_state_change = st_folium(
        width=620, height=580,

    if map_state_change['last_clicked']:
        loc = map_state_change['last_clicked']
        st.session_state.location = folium.Marker([loc['lat'], loc['lng']])
        fg.clear_layers()  # Clear existing markers
        fg.add_child(st.session_state.location)  # Add the new marker

if anyone could point me to a tutorial or indicate how to fix the issue I would really appreciate! thanks!

I managed to add a marker to each click but I have no idea how to make the previous marker dissapear…

    import streamlit as st
    import folium
    from streamlit_folium import folium_static

    def get_pos(lat, lng):
        return lat, lng

    # Create a Folium map
    m = folium.Map(location=[46.903354, 1.888334], zoom_start=6)

    # When the user interacts with the map
    map = st_folium(
        width=620, height=580,
    data = None
    if map.get("last_clicked"):
        data = get_pos(map["last_clicked"]["lat"], map["last_clicked"]["lng"])

    if data is not None: