Skip to content

DrawControl

Example

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

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

  return (
    <div className="map-wrapper">
      <MbMap center={mapCenter} zoom={zoom}>
        <MbTiandituLayer types={['vec']} />
        <MbDrawControl position="top-right" />
      </MbMap>
    </div>
  )
}

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

API

PROPS

NameDescriptionTypeDefault
functionsFunctionsarray("point", "polyline", "polygon", "trash")["point", "polyline", "polygon", "trash"]
positionPositionstring('top-left' , 'top-right' , 'bottom-left' ,'bottom-right')top-right

EVENTS

NameDescriptionParameters
onCreatedMap initialization completed event-
onDrawCreatedDrawing completed eventfeatures: AnyArr
onDrawDeleteDelete element(s) eventfeatures: AnyArr
onDrawUpdateDrawing element(s) updated eventobject — Contains action property and features

METHODS

NameDescriptionDefinition
addReturns: Array — featureIdsgeojson: object — geojson
getReturns: object — feature geojsonfeatureId: string
getSelectedIdsGets the IDs of the currently selected features() => string[]
getSelectedGets the currently selected features in GeoJSON format (as a FeatureCollection)() => object — featureCollection
getAllGets all drawn features in GeoJSON format (as a FeatureCollection)() => object — geojson featureCollection
deleteDeletes features based on their IDs(ids: string[]) => void
deleteAllDeletes all drawn features() => void