How do you call fitBounds() when using leaflet-react?

后端 未结 2 871
暗喜
暗喜 2020-12-16 01:12

I cannot figure out how to call fitBounds() on the Leaflet map.

If I was just using vanilla leaflet, this solution would work perfectly: Zoom to fit all markers in M

2条回答
  •  半阙折子戏
    2020-12-16 01:38

    Here is an example how to accomplish it via react-leaflet

    handleClick() {
        const map = this.mapRef.current.leafletElement;  //get native Map instance
        const group = this.groupRef.current.leafletElement; //get native featureGroup instance
        map.fitBounds(group.getBounds());
    }
    

    where

    {this.props.locations.map(location => (

    {location.name}

    ))}

    which corresponds to

    var leafletMap = new L.featureGroup([marker1, marker2, marker3]);
    map.fitBounds(leafletMap.getBounds());
    

    Here is a demo

提交回复
热议问题