Set parameter from click on map feature

I would like to use the identification of a point selected on a map to filter data from a table to show in a graph.
I modified the example from User input, fields and buttons - Map features | VIKTOR Documentation
But it does not return the selected point as a parameter for generate_plotly_view to use.

class Parametrization(ViktorParametrization):
    introduction = Text()
    select_button = SetParamsButton('Select point from map',
                                    interaction=MapSelectInteraction('get_map_view', max_select=1))

class Controller(ViktorController):
    label = 'My Data Science App'
    parametrization = Parametrization

    @MapView('Map', duration_guess=1)
    def get_map_view(self, params, **kwargs):
        # Create some points using coordinates
        data = pd.read_csv('data.csv')

        features = []
        for i, row in data.iterrows():
                MapPoint(row['lat'], row['lon'], identifier=row['id'],
        return MapResult(features)

    def set_point_from_selection(self, params, event, **kwargs):
        params.selected_point = event.value[0]
        return SetParamsResult({'selected_point': params.selected_point})

    @PlotlyView('Plots', duration_guess=1)
    def generate_plotly_view(self, params, **kwargs):
        data = data[data['id'] == params.selected_point]

Hi Thomas, welcome to the forum!

Your example misses the selected_point parameter so the params is never really set.

Something along the lines of this:

def get_locations(params, **kwargs):
    options = ....  # add logic to extract options from csv
    return options

selected_point = Optionfield('selected point', options=get_locations)

Does that help?

The following docs section also contain an example implementation if you get stuck:

Hi Matthijs,
Thank you for the reply.
I would like to set the filter by clicking a point on the map and retrieving the identification of that point. I do not want to select it in a dropdown menu.
My full code is in VIKTOR if that helps.

You set the filter by clicking points on the map (after clicking the SetParamsButton). However it is also possible to select it in the dropdown. My code sniplet only dealt with the OptionField part, you should still keep the SetParamsButton.

The SetParamsButton needs a field to set

Then my question is how to connect the Controller to the Parametrization in the other direction than usual or how to add the variable to the parameters within Controller.

Did you try the example from the docs that i linked above? That is complete and should be a good starting point which you can use to tweak to your usecase

The only real difference I see with the example you linked above is the OptionField. The OptionField creates a dropdown menu. I don’t want a dropdown menu. I want to select a point on the map and use that for filtering.
Using a MapSelectInteraction with a SetParamsResult does not add a key value pair to my params.

The question is a bit what you want to do with the resulting selection. You have a few options:

  • store the selection on the params using a SetParamsButton. For this you need a field (e.g. an OptionField). Btw, that field can be invisible, allthough im not sure this gives a nice user experience
  • store the selection on Storage. You can use a normal AnalyseButton
  • perform a download using a DownloadButton

I hope this helps

Thank you Matthijs. The issue remains unsolved.

Is it possible to show the result in an OutputField or something similar? I would like to give the user more information of the selected item. If so, how?