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
Name | Description | Type | Default |
---|---|---|---|
id | Layer ID | string | - |
color | Color value. Supports modes such as "#000000", "rgb(0,0,0)", [255,0,0,1] | string / number[] | #000000 |
data | Data 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/polygon | VectorLayerData | - |
geoJsonDataSource | GeoJSON 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 ignored | string / GeoJSONSource | - |
opacity | Opacity | number | 1 |
outlineColor | Outline color | string / number[] | #000 |
maxzoom | Maximum zoom level | number | 22 |
minzoom | Minimum zoom level | number | 0 |
pickable | Whether the layer responds to pick events. If false , the component will not emit mouse-related events | boolean | true |
show | Whether to show the layer | boolean | true |
sourceId | ID of the layer's source. If this ID is set, both the geoJsonDataSource and data properties are ignored | string | - |
sourceLayerName | Layer name in the source data. This property is effective when sourceId is set | string | - |
translate | Offset. Positive numbers indicate right/down | number[] | [0,0] |
pattern | Image texture. The name of the image loaded via ImageLoader. If this value is set, the color property is ineffective | string | - |
autoHighlight | Whether to automatically apply highlighting | boolean | false |
highlightColor | The highlight color | string / number[] | [255, 255, 128, 1] |
generateId | Whether to automatically generate GeoJSON Feature IDs. If the data does not have IDs and automatic highlighting is required, this must be set to true | boolean | false |
useExpression | Whether to use an expression. When set to true, color-related properties will not be parsed | boolean | false |
EVENTS
Name | Description | Parameters |
---|---|---|
onCreated | Map initialization completed event | - |
onClick | Layer click event | object — Contains screen coordinates (pixel), longitude/latitude (coordinate), selected element properties, and originalEvent |
onMouseMove | Mouse move event | object — Contains screen coordinates (pixel), longitude/latitude (coordinate), selected element properties, and originalEvent |
onMouseLeave | Mouse leave element event | MapMouseEvent |
METHODS
Name | Description | Definition |
---|---|---|
exportToGeoJson | Translate 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 |