Skip to content

PolygonLayer

Example

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

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

  const colors = ['rgba(255,125,0,0.5)', '#ec71a9', 'red']
  const [colorIndex, setColorIndex] = useState(0)
  const polygonCoordinates = [
    {
      coordinates: [
        [110.56640625, 39.639537564366684],
        [107.22656249999999, 36.527294814546245],
        [107.57812499999999, 32.39851580247402],
        [112.5, 30.600093873550072],
        [117.42187500000001, 32.02670629333614],
        [117.24609374999999, 36.94989178681327],
        [114.697265625, 39.70718665682654],
        [110.56640625, 39.639537564366684],
      ],
    },
    {
      coordinates: [
        [82.08984375, 32.47269502206151],
        [90.966796875, 32.47269502206151],
        [90.966796875, 38.685509760012],
        [82.08984375, 38.685509760012],
        [82.08984375, 32.47269502206151],
      ],
    },
  ]

  return (
    <div className="map-wrapper">
      <MbMap center={mapCenter} zoom={zoom}>
        <div style={{ position: 'absolute', top: '5px', left: '5px' }}>
          <button className="primary" onClick={() => setColorIndex((c) => ++c)}>
            Switch Color
          </button>
        </div>
        <MbTiandituLayer types={['vec']} />
        <MbPolygonLayer
          data={polygonCoordinates}
          color={colors[colorIndex % colors.length]}
          outlineColor="yellow"
        />
      </MbMap>
    </div>
  )
}

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

API

PROPS

NameDescriptionTypeDefault
idLayer IDstring-
colorColor value. Supports modes such as "#000000", "rgb(0,0,0)", [255,0,0,1]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-
opacityOpacitynumber1
outlineColorOutline colorstring / number[]#000
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-
translateOffset. Positive numbers indicate right/downnumber[][0,0]
patternImage texture. The name of the image loaded via ImageLoader. If this value is set, the color property is ineffectivestring-
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
useExpressionWhether to use an expression. When set to true, color-related properties will not be parsedbooleanfalse

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