Skip to content

3D蜂窝热力图(HexagonLayer)

示例

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

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

  const data = 'https://mapbox-web.github.io/mapbox-react/geojson/beijing_price.json'
  const [radius, setRadius] = useState(1000)

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

  return (
    <div className="map-wrapper">
      <MbMap ref={mapInst} center={mapCenter} zoom={zoom} pitch={pitch}>
        <MbTiandituLayer types={['vec']} />
        <MbHexagonLayer
          data={data}
          radius={radius}
          position={(d) => d.lnglat}
          colorWeight={(d) => d.transPrice}
          colorAggregation="MEAN"
          elevationWeight={(d) => d.transPrice}
          elevationAggregation="MEAN"
          pickable={true}
          elevationScale={50}
          onMouseMove={mousemoveHandler}
        />
      </MbMap>
    </div>
  )
}

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

API

PROPS

名称描述类型默认值
id图层Idstring-
radius半径,单位为米number1000
position-function-
autoHighlight本属性为true且pickable为true时,则会将鼠标悬浮选中的对象设为高亮booleanfalse
colorAggregation格网颜色聚合模式 本属性与colorWeight一起设置时生效string(SUM / MEAN / MIN / MAX)SUM
colorRange颜色范围string[][ "rgb(255, 255, 178)", "rgb(254, 217, 118)", "rgb(254, 178, 76)", "rgb(253, 141, 60)", "rgb(240, 59, 32)", "rgb(189, 0, 38)", ]
colorValue六边形颜色隐射函数 例如:根据六边形内点的数量来计算颜色(密度热力图) (points) => points.length 若设置了本属性,则 colorWeight与colorAggregation 失效AnyFunc-
colorWeight格网中每一点的颜色权重值计算函数 本属性与colorAggregation一起设置时生效AnyFunc-
coverage蜂窝网格的覆盖范围比例,值在0-1之间,最后呈现的网格半径将为radius*coveragenumber1
data图层源数据array / object / promise / string[]
elevationAggregation高度聚合模式stringSUM
elevationScale蜂窝体放大倍数number1
elevationValue蜂窝体放高度估算函数 例如:根据六边形内点的数量来计算高度 (points) => points.length 若设置了本属性,则 elevationWeight 与 elevationAggregation 失效AnyFunc-
elevationWeight高度权重值计算函数AnyFunc() => 1
extruded是否为3D模式booleantrue
highlightColor要与高亮对象原始颜色进行混合(blend)的颜色值string / number[][255, 255, 128, 1]
highlightedObjectIndex高亮元素序号number-1
material是否有材质booleantrue
opacity透明度number1
pickable图层是否会响应鼠标事件,若为false,则组件不会emit鼠标相关事件booleanfalse
show是否显示booleantrue

EVENTS

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

METHODS

名称描述定义