Leaflet在React应用程序中使用新数据重新渲染地图

问题描述 投票:0回答:1

鉴于以下实施:

getCurrentMap() {
  const {currentlyActiveMap: {task, type, layout}} = this.mapStore
  if (this.currentMap) this.currentMap.eachLayer(layer => {
    this.currentMap.removeLayer(layer)
  })

  let centerX = 0
  let centerY = 0

  if (layout.image_width > layout.image_height) {
    centerX = layout.tile_size / 2
    centerY = (layout.tile_size * layout.image_height / layout.image_width) / 2
  } else {
    centerX = (layout.tile_size * layout.image_width / layout.image_height) / 2
    centerY = layout.tile_size / 2
  }

  this.currentMap = this.currentMap || Leaflet.map(this.map, {
    center: [-centerY, centerX],
    crs: Leaflet.CRS.Simple,
    minZoom: 0,
    maxZoom: layout.max_zoom_level + 1,
    maxBounds: [[layout.tile_size, -layout.tile_size], [-2 * layout.tile_size, 2 * layout.tile_size]],
    doubleClickZoom: false,
    attributionControl: false,
    zoom: 2
  })

  let currentLayer = Leaflet.tileLayer(`${layout.tiling_url}`)
  this.currentMap.addLayer(currentLayer)

  return (
    <Fragment>
      <button type='button' className={styles.mapPanelCreateButton} onClick={this.handleCreateTask}>
        <FaPlus/>
      </button>
    </Fragment>
  )
}


render() {
  const mapLayout = !!this.taskStore.loading || !this.mapStore.currentlyActiveMap
    ? <LoadingContainer transparent={true}/>
    : this.getCurrentMap()

  return (
    <Fragment>
      <div id='map' className={styles.mapPanelContainer} ref={map => this.map = map}></div>
      {mapLayout}
    </Fragment>
  )
}

我无法渲染地图,我也无法更新/交换或以其他方式更改地图中的数据(需要在自定义平铺图像和全局地图之间切换)。 Ids在这个实现中存在一些固有的错误,还是有更好的案例来实现这个目标?

一如既往地感谢任何方向,所以提前感谢!

另一个实现具有相同的结果:

Leaflet.tileLayer(`${layout.tiling_url}`).addTo(this.currentMap)

编辑:我也收到臭名昭着的:未捕获错误:地图容器已初始化。

reactjs leaflet maps
1个回答
0
投票

所以我想我会回头并提供我的解决方案:

@jbccollins感谢您的建议,但我需要更多低级别的定制,如果没有PR,反应传单无法提供。我很乐意做出贡献,但我没有时间等待收录。

this.locationLayer = Leaflet.tileLayer('https://{s}.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}{r}.png')
this.floorPlanLayer = Leaflet.tileLayer(layout.tiling_url)

this.currentMap = Leaflet.map(this.map, options)
this.locationLayer.addTo(this.currentMap)

if (this.currentMap.hasLayer(this.locationLayer)) {
    this.currentMap.addLayer(this.floorPlanLayer)
    this.currentMap.removeLayer(this.locationLayer)
    this.currentLayer = this.floorPlanLayer
  } else {
    this.currentMap.addLayer(this.locationLayer)
    this.currentMap.removeLayer(this.floorPlanLayer)
    this.currentLayer = this.locationLayer
  }
© www.soinside.com 2019 - 2024. All rights reserved.