给定以下实现:
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)
编辑:我也收到了臭名昭著的:未捕获错误:映射容器已初始化。
所以我想我会回过头来提供我的解决方案:
@jbccollins感谢您的建议,但我需要更多的低级自定义,如果没有 PR,react-leaflet 就无法提供这些自定义。 我很想做出贡献,但我只是没有时间等待包含。
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
}