【问题标题】:How to get bounds with react-leaflet如何使用 react-leaflet 获得界限
【发布时间】:2016-11-21 20:20:04
【问题描述】:

我想获取当前地图的边界,以便我可以使用 Overpass API 搜索这些边界。

对于传单我知道方法只是 map.getBounds(),但我不知道如何在 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.refs.map.getBounds 不是函数。

【问题讨论】:

    标签: reactjs leaflet react-leaflet


    【解决方案1】:

    试试this.refs.map.leafletElement.getBounds

    根据documentation

    您可以直接访问组件创建的 Leaflet 元素 在此组件中使用 this.leafletElement。这个传单元素 通常在 componentWillMount() 中创建,除了 Map 只有在容器被创建后才能创建的组件 渲染。

    这是一种说法,他们将传单对象作为leafletElement 属性存储在其组件对象上。

    【讨论】:

    • 我有点不明白except for the Map component where it can only be created after the container is rendered. 的部分,我可以为每个渲染获得新的界限吗?谢谢
    • componentDidMountcomponentDidUpdate访问它
    • 如果这个答案不能解决问题,你也可以看看stackoverflow.com/questions/51399480/…(对我来说,ref='map' 部分丢失了)
    • refs 已弃用
    猜你喜欢
    • 2010-11-29
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    相关资源
    最近更新 更多