pythonleafletplotly-dashdash-leaflet

Get the bounding box of the visible dash-leaflet map?


This SO question contains an explanation for how to dynamically get the extents of a Leaflet map.

I wonder if there is a way of doing this with dash-leaflet, so I can get this in a callback every time a map updates.


Solution

  • You can get this information via the bounds property of the map object. Here is a small example,

    import json
    import dash_leaflet as dl
    from dash import html, Input, Output, Dash
    
    app = Dash()
    app.layout = html.Div([
        dl.Map(dl.TileLayer(), style={'width': '1000px', 'height': '500px'}, id="map"),
        html.Div(id="log")
    ])
    
    @app.callback(Output("log", "children"), Input("map", "bounds"))
    def log_bounds(bounds):
        return json.dumps(bounds)
    
    if __name__ == '__main__':
        app.run_server()
    

    Example app