Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get bounds with react-leaflet

I want to get bounds of the current map so that I can search those bounds with the Overpass API.

For leaflet I know the method is just map.getBounds(), but I don't know how to implement that in react-leaflet.

class SimpleExample extends React.Component {
  constructor() {
    super();
    this.state = {
      lat: 51.505,
      lng: -0.09,
      zoom: 13,
    };
  }

  componentDidMount() {
    console.log(this.refs.map.getBounds())
  }

  render() {
    const position = [this.state.lat, this.state.lng];
    return (
      <Map center={position} zoom={this.state.zoom} ref='map'>
        <TileLayer
          attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
          url='http://{s}.tile.osm.org/{z}/{x}/{y}.png'
        />
      </Map>
    );
  }
}

This is what I've tried. Error says that this.refs.map.getBounds isn't a function.

like image 788
Jake Hm Avatar asked Nov 21 '16 20:11

Jake Hm


People also ask

How to set the maxbounds of a Leaflet map?

When you instantiate your Leaflet map, you just need to pass in a maxBounds option among your map options. When this option is set, the map restricts the view to the given geographical bounds, bouncing the user back when he tries to pan outside the view. To set the restriction dynamically, use setMaxBounds method.

Does leaflet ReactJS map show tile completely?

Leaflet ReactJS Map does not show tile completely Hot Network Questions Print a NxN integer involute What is the greatest possible value of a^b^c^d? Can people generate quintessence through prayer? Manipulating text using shell script: How can I fill in "missing" lines? Can you infinitely bass boost a song?

How to create leaflet element in a component?

This leaflet element is usually created in componentWillMount(), except for the Map component where it can only be created after the container is rendered. which is a round about way of saying they store the leaflet object as leafletElementproperty on their component objects. Share Follow edited Sep 15 '19 at 0:11


1 Answers

Try this.refs.map.leafletElement.getBounds.

According to the documentation:

You can directly access the Leaflet element created by a component using this.leafletElement in this component. This leaflet element is usually created in componentWillMount(), except for the Map component where it can only be created after the container is rendered.

which is a round about way of saying they store the leaflet object as leafletElement property on their component objects.

like image 85
Brandon Avatar answered Sep 28 '22 05:09

Brandon