Python Dash Mapboxgl多边形点击事件数据,

问题描述 投票:0回答:1

如何从python破折号/图线中的mapboxgl多边形单击事件中获取数据(geojson属性-名称,ID等)?

以下是破折号图元素

layer = dict(
        type="fill",
        below='traces',
        color="#18607e",
        opacity=0.7,
        hovermode="closest",
        interactive=True,
        text=[x['properties']['id'] for x in geojson['features']],
        source=geojson,
        sourcetype="geojson"

    )

    element = dcc.Graph(
                id='TxWCD-choropleth',
                figure=dict(
                    data=[dict(
                        type='scattermapbox'
                    )],
                    layout=dict(
                            plot_bgcolor="#18607e",
                            paper_bgcolor="#18607e",
                            clickmode="event+select",
                            mapbox=dict(
                                layers=[layer],
                                accesstoken=_token,
                                center=dict(
                                    lat=53.350140,
                                    lon=-6.266155
                                ),
                                zoom=1,
                                style='light'
                            ),
                            height=600,
                            autosize=True,
                            margin=dict(
                                l=0,
                                r=0,
                                b=0,
                                t=0,
                                pad=4
                            )
                    )
                )
            )

回叫事件:


app.callback(
    Output(component_id='graphs', component_property='children'),
    [Input('map-flex', "n_clicks")]
)
def update_graph(data):

    # Do some updates
    # Expected result: mapbox click event data geojson properties

    return ''

预期结果:

单击Mapbox多边形后,返回Mapbox事件数据。即geojson属性。

感谢您提供任何帮助或解决方法。谢谢

plotly mapbox-gl-js plotly-dash
1个回答
0
投票

查看scattermapbox的文档。有一个名为customdata的属性,可用于确定将什么传递给回调。

要定义回调,您可以使用此reference。本质上,您正在执行以下步骤:

  1. 将clickmode属性添加到图形布局:

    'layout': {
        'clickmode': 'event+select'
    }
    
  2. 定义您的回叫:

    @app.callback(
        Output(component_id='graphs', component_property='children'),
        [Input('map-flex', 'clickData')]) 
    def display_click_data(custom_data):
        print(custom_data)
    
© www.soinside.com 2019 - 2024. All rights reserved.