Skip to content

CircleLayer

Circle layer, which can also be used as a point when the radius is small.

Example

Example Source Code
tsx
import React, { useEffect, useRef, useState } from 'react'
import ReactDOM from 'react-dom'
import { MbCircleLayer, MbMap, MbTiandituLayer } from '@mapbox-react/core'
import type { VectorLayerData } from '@mapbox-react/core'

const App = () => {
  const [mapCenter] = useState([116.38745, 39.91266])
  const [zoom, setZoom] = useState(2)

  const [strokeColor, setStrokeColor] = useState('#000000')
  const [blur, setBlur] = useState(0)
  const [pickable, setPickable] = useState(true)
  const [circle, setCircle] = useState({
    airpotDataSource: [] as VectorLayerData,
    color: [
      'scalerank',
      1,
      'rgb(178,24,43)',
      2,
      'rgb(239,138,98)',
      4,
      'rgb(253,219,199)',
      6,
      'rgb(209,229,240)',
      8,
      'rgb(103,169,207)',
      10,
      'rgba(33,102,172,0)',
    ],
    radius: ['scalerank', 1, 10, 10, 1],
  })

  const loadCircleData = () => {
    setCircle((c) => ({ ...c, airpotDataSource: [] }))
    fetch(
      'https://mapbox-web.github.io/mapbox-react/geojson/ne_10m_airports.geojson'
    )
      .then((res) => res.json())
      .then((data) => {
        const airData = data.features.map(
          (feature: {
            geometry: { coordinates: string | any[] }
            properties: any
          }) => {
            return {
              coordinates: feature.geometry.coordinates.slice(),
              properties: feature.properties,
            }
          }
        )
        setCircle((c) => ({ ...c, airpotDataSource: [...airData] }))
      })
  }

  useEffect(() => {
    loadCircleData()
  }, [])

  return (
    <div className="map-wrapper">
      <MbMap center={mapCenter} zoom={zoom}>
        <MbTiandituLayer types={['vec']} />
        <MbCircleLayer
          {...circle}
          data={circle.airpotDataSource}
          color={circle.color}
          radius={circle.radius}
          blur={blur}
          pickable={pickable}
          strokeWidth={1}
          strokeOpacity={1}
          strokeColor={strokeColor}
          onCreated={() => console.log('created circle layer')}
          onClick={(args) => console.log('click', args)}
          onMouseMove={(args) => console.log('onMouseMove', args)}
        />
      </MbMap>
    </div>
  )
}

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

API

PROPS

NameDescriptionTypeDefault
idLayer IDstring-
radiusRadius. ['mag',1,5,14,20] indicates that the circle radius is set according to the mag property value. When the value is 1/14, the circle radius is 5/20. [1,5,14,20] indicates that when the map zoom value is 1/14, the circle radius is 5/20number / Array<string | number>5
blurCircle blur amount. A value of 1 means only the center of the circle is opaquenumber0
colorColor value. Can be set to a number or an array. ['mag',1,"rgba(33,102,172,0)",14,"rgb(253,219,199)"] indicates that the color value is set according to the mag property value. When the value is 1/14, the color value is rgba(33,102,172,0)/rgb(253,219,199)string / Array<string | number>#000000
dataData for the vector layer. Each object in the array must contain the required coordinates property and the optional properties property. Each object represents a point/line/polygonVectorLayerData-
geoJsonDataSourceGeoJSON data source. Can be set to a GeoJSON data link or a GeoJSON data object. Specification Link. If this property is set, the data property is ignoredstring / GeoJSONSource-
opacityOpacity. The default value is 1. If set as an array, it is associated with zoom. For example: [7,1,9,0] means the opacity is 1 from zoom level 0 to 7, and 0 above zoom level 9number / number[]1
maxzoomMaximum zoom levelnumber22
minzoomMinimum zoom levelnumber0
pickableWhether the layer responds to pick events. If false, the component will not emit mouse-related eventsbooleantrue
showWhether to show the layerbooleantrue
sourceIdID of the layer's source. If this ID is set, both the geoJsonDataSource and data properties are ignoredstring-
sourceLayerNameLayer name in the source data. This property is effective when sourceId is setstring-
strokeColorCircle border color. Can be set to a number or an array. ['mag',1,"rgba(33,102,172,0)",14,"rgb(253,219,199)"] indicates that the color value is set according to the mag property value. When the value is 1/14, the color value is rgba(33,102,172,0)/rgb(253,219,199)string / Array<string | number>#000000
strokeOpacityCircle border opacitynumber0
strokeWidthCircle border width in pixelsnumber0
translateAnchor point translation. Positive direction is right/down in pixelsnumber[][0,0]
autoHighlightWhether to automatically apply highlightingbooleanfalse
highlightColorThe highlight colorstring / number[][255, 255, 128, 1]
generateIdWhether to automatically generate GeoJSON Feature IDs. If the data does not have IDs and automatic highlighting is required, this must be set to truebooleanfalse

EVENTS

NameDescriptionParameters
onCreatedMap initialization completed event-
onClickLayer click eventobject — Contains screen coordinates (pixel), longitude/latitude (coordinate), selected element properties, and originalEvent
onMouseMoveMouse move eventobject — Contains screen coordinates (pixel), longitude/latitude (coordinate), selected element properties, and originalEvent
onMouseLeaveMouse leave element eventMapMouseEvent

METHODS

NameDescriptionDefinition
exportToGeoJsonTranslate the layer's data to GeoJSON formatted text. If the layer's data source comes from a source component or a URL, output null.()=> object | null