Skip to content

3D棱柱图层(GridCellLayer)

示例

示例源码
tsx
import React, { useEffect, useRef, useState } from 'react'
import ReactDOM from 'react-dom'
import { MbMap, MbTiandituLayer } from '@mapbox-react/core'
import { MbGridCellLayer } from '@mapbox-react/deck-layers'

const App = () => {
  const [mapCenter] = useState([116.314177, 39.832819])
  const [zoom, setZoom] = useState(10)
  const [pitch, setPitch] = useState(45)
  const mapInst = useRef<any>()

  const data = 'https://mapbox-web.github.io/mapbox-react/geojson/beijing_price.json'
  const position = (d: { lnglat: any }) => d.lnglat
  const elevation = (d: { transPrice: number }) => d.transPrice / 30
  const fillColor = (d: { transPrice: number }) =>
    `rgba(${(d.transPrice / 10000) * 15},125,125,0.8)`
  const [cellSize, setCellSize] = useState(150)

  const mousemoveHandler = ({ object }: { object: any }) => {
    console.log(object)
  }
  const mouseleaveHandler = () => {
    console.log('mouseleaveHandler')
  }

  return (
    <div className="map-wrapper">
      <MbMap ref={mapInst} center={mapCenter} zoom={zoom} pitch={pitch}>
        <MbTiandituLayer types={['vec']} />
        <MbGridCellLayer
          data={data}
          position={position}
          elevation={elevation}
          fillColor={fillColor}
          cellSize={cellSize}
          pickable={true}
          autoHighlight={true}
          onMouseMove={mousemoveHandler}
          onMouseLeave={mouseleaveHandler}
        />
      </MbMap>
    </div>
  )
}

ReactDOM.render(<App />, document.querySelector('#root'))

API

PROPS

名称描述类型默认值
id图层Idstring-
position位置信息function-
autoHighlight本属性为true且pickable为true时,则会将鼠标悬浮选中的对象设为高亮booleanfalse
cellSize半径,单位为米number1000
data图层源数据string / IndexAny / AnyArr / Promise<any>''
elevation高程number / AnyNumberFunc1000
extruded是否为3D模式booleantrue
fillColor填充颜色string / AnyFuncblack
filled是否填充booleantrue
highlightColor要与高亮对象原始颜色进行混合(blend)的颜色值string / number[][255, 255, 128, 1]
highlightedObjectIndex高亮元素序号number-1
lineColor线颜色string / AnyFuncblack
lineWidth线宽度string / AnyNumberFunc1
lineWidthMaxPixels-numberNumber.MAX_SAFE_INTEGER
lineWidthMinPixels-number0
lineWidthScale-number1
lineWidthUnits-string(pixels / meters)meters
opacity透明度number1
pickable图层是否会响应鼠标事件,若为false,则组件不会emit鼠标相关事件booleanfalse
show是否显示booleantrue
stroked是否描边booleanfalse

EVENTS

名称描述参数
onCreated初始化完成事件-
onMouseMove-{ object: any; coordinates: mapboxgl.LngLat; pixel: [number, number] }
onMouseLeave--
onClick-{ object: any; coordinates: mapboxgl.LngLat; pixel: [number, number] }

METHODS

名称描述定义