folium timeslider doesn't remove previous added marker in the map

Viewed 1323

I am trying to plot markers using time slider. I have created timestamped geojson as follows:

def create_geojson_features(df):

    features = []
    for lat,lan,intensity,time in zip(df['latitude'],df['longitude'],df['intensity'],df['timestamp']): 
        time = str(time)
        feature = {
            'type': 'Feature',
            'geometry': {
                'type':'Point', 
                'coordinates':[lan,lat]
            },
            'properties': {
                'time': time,
                'style': {'Color' : color(intensity)},
                'icon': 'Marker',
                'color':color(intensity),
                'iconstyle':{
                    'Color': color(intensity),
                    'fillOpacity': 0.8,
                    'stroke': 'true',
                    'radius': 7
                }
            }
        }
        features.append(feature)
    return features
features = create_geojson_features(df)

Here is my code to plot the markers

from folium.plugins import TimestampedGeoJson
m = folium.Map([latmean,lonmean], zoom_start=11)

TimestampedGeoJson(
        {'type': 'FeatureCollection',
        'features': features}
        , period='PT1H'
        , add_last_point=False
        , auto_play=False
        , loop=False
        , max_speed=1
        , loop_button=True
        , date_options='YYYY/MM/DD HH:mm:ss'
        , time_slider_drag_update=True
    ).add_to(m)

I have specified period as PT1H, as I scroll the slider the new points keep on adding. How can I remove old points and show new points only? Please help.

1 Answers

There's a parameter just for that: duration = 'P1D'

With your example, and to only display per timestamp:

TimestampedGeoJson(
        {'type': 'FeatureCollection',
        'features': features}
        , period='PT1H'
        , duration='PT1H'
        , add_last_point=False
        , auto_play=False
        , loop=False
        , max_speed=1
        , loop_button=True
        , date_options='YYYY/MM/DD HH:mm:ss'
        , time_slider_drag_update=True
    ).add_to(m)
Related