在破折号传单地图中标记选定的州

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

我正在创建美国地图,我想让它具有交互性。如果用户单击某个状态,它会突出显示,如果单击另一个状态,它也会突出显示。最后,如果用户点击了高亮状态,高亮将被移除。我想不出要更改 @app.callback 中的哪个属性才能完成这项工作。 这是构建美国地图的代码:

`import dash_leaflet as dl
from dash import Dash, html, Output, Input, State
from dash_extensions.javascript import arrow_function

app = Dash()
app.layout = html.Div([
    dl.Map(center=[39, -98], zoom=4, children=[
        dl.TileLayer(),
        dl.GeoJSON(url="/assets/us-states.pbf", format="geobuf", id="states",
                   hoverStyle=arrow_function(dict(weight=5, color='#666', dashArray='')),
                   options=dict(hidden=True)),  # geobuf resource (fastest option)
    ], style={'width': '100%', 'height': '50vh', 'margin': "auto", "display": "block"}, id="map"),
    html.Div(id="state"), html.Div(id="capital")
])


@app.callback(Output("state", "children"), [Input("states", "click_feature")])
def capital_click(feature):
    if feature is not None:
        return f"You clicked {feature['properties']['name']}"

if __name__ == '__main__':
    app.run_server(port=8052, debug=True)`

这里是上面代码的输出: 请注意,我点击了加利福尼亚以返回图片下方的文字 Map Image

python plotly-dash plotly-python dash-leaflet
© www.soinside.com 2019 - 2024. All rights reserved.