Skip to content

数据源(Source)

支持设置GeoJson、TileJson数据

示例

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

const App = () => {
  const [mapCenter] = useState([120.30782359643746, 31.49292271319491])
  const [zoom, setZoom] = useState(11)

  const [showLayer, setShowLayer] = useState(false)
  const geoJsonSource = {
    type: 'geojson',
    data: `https://mapbox-web.github.io/mapbox-react/geojson/wuxi.geojson`,
  }

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

        {showLayer && (
          <MbPolygonLayer sourceId="sourceExample" opacity={0.5} color="blue" />
        )}

        <MbSource
          id="sourceExample"
          geoJsonSource={geoJsonSource}
          onSourceAdded={() => setShowLayer(true)}
        />
      </MbMap>
    </div>
  )
}

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

API

PROPS

名称描述类型默认值
id数据源Idstring-
geoJsonSource参考:GeoJsonIndexAny-
tileJsonSource参考:TileJsonIndexAny-

EVENTS

名称描述参数
onSourceAdded数据源添加完成事件source

METHODS

名称描述定义