jupyter-widgets / ipyleaflet

A Jupyter - Leaflet.js bridge
https://ipyleaflet.readthedocs.io
MIT License
1.49k stars 365 forks source link

How to use fit_bounds? #1088

Open aborruso opened 1 year ago

aborruso commented 1 year ago

Hi, trying to use fit_bounds, I have no results: the map stays in its starting center and zoom level.

How to use properly fit_bounds? I'm using it in a Python Jupyter Notebook.

Thank you

import os
import json
import Fiona

from ipyleaflet import Map, GeoJSON

with open('map.geojson', 'r') as f:
    data = json.load(f)

fi = fiona.open("map.geojson")

m = Map(center=(38,13), zoom=7)
sw = [fi.bounds[1], fi.bounds[0]]
ne = [fi.bounds[3], fi.bounds[2]]

geo_json = GeoJSON(
    data=data
)

m.add_layer(geo_json)

m.fit_bounds([sw, ne])

m
{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "coordinates": [
          [
            [
              13.87662269944002,
              37.894561129952606
            ],
            [
              13.87662269944002,
              37.53137739067412
            ],
            [
              14.34737227435636,
              37.53137739067412
            ],
            [
              14.34737227435636,
              37.894561129952606
            ],
            [
              13.87662269944002,
              37.894561129952606
            ]
          ]
        ],
        "type": "Polygon"
      }
    }
  ]
}