Reputation: 75
I want to get this map bounds and center it
map
I tried with getBounds() and getCenter() but it was undefined.
then i found this but it says Cannot read property 'leafletElement' of undefined
Thanks for your help in forward.
class FortMap extends Component {
state = {
lat: 51.505,
lng: -0.09,
zoom: 18,
}
componentDidMount(){
console.log(this.refs.map.leafletElement.getBounds);
}
render() {
const { lat , lng , zoom } = this.state;
const position = [0, 0];
return (
<Map center={position} zoom={zoom}>
<TileLayer
url={fortniteMap}
attribution="© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors"
/>
</Map>
)
}
}
export default FortMap;
Upvotes: 7
Views: 7039
Reputation: 59318
Seems you forgot to assign ref
attribute for a Map
component:
<Map ref='map' center={position} zoom={zoom}>
...
</Map>
to get a reference to leaflet instance:
componentDidMount(){
let mapInst = this.refs.map.leafletElement;
}
Upvotes: 5