Skip to content

气泡弹窗(MapPopup)

示例

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

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

  const [showCustomPopup, setShowCustomPopup] = useState(true)
  const [showDefaultPopup, setShowDefaultPopup] = useState(true)
  const [coordinates, setCoordinates] = useState([116.388, 39.91266])
  const [hideOnZoom, setHideOnZoom] = useState(false)
  const [hideOnMove, setHideOnMove] = useState(false)

  return (
    <div className="map-wrapper">
      <MbMap center={mapCenter} zoom={zoom} pitch={10}>
        <MbTiandituLayer types={['vec']} />
        <MbMapPopup
          show={showCustomPopup}
          coordinates={coordinates}
          anchor="bottom"
          showTip={false}
          hideOnZoom={hideOnZoom}
          hideOnMove={hideOnMove}
          onUpdateShow={setShowCustomPopup}
        >
          <div
            style={{ width: '100px', height: '100px', background: '#81adb0d9' }}
          >
            <p>自定义Popup</p>
          </div>
        </MbMapPopup>

        <MbMapPopup
          show={showDefaultPopup}
          coordinates={[116.4, 39.91266]}
          hideOnClick={false}
          content={<>默认Popup</>}
          onUpdateShow={setShowDefaultPopup}
        ></MbMapPopup>
      </MbMap>
    </div>
  )
}

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

API

PROPS

名称描述类型默认值
anchor表示标记的哪个部位距离坐标点最近string('center', 'top', 'bottom', 'left', 'right', 'top-left', 'top-right', 'bottom-left', 'bottom-right')-
className添加到popup dom元素上的Classstring-
coordinatesmarker的经纬度位置number[]-
hideOnClick点击地图时是否自动关闭气泡弹窗booleanfalse
hideOnMove鼠标在地图移动时是否自动关闭气泡弹窗booleanfalse
offsetpopup偏移量 正向为右/下number / number[][0,0]
show是否显示booleantrue
showTip是否自动添加气泡指向三角形booleantrue
tipColor指向三角形颜色string#fff
content自定义提示框内容ReactNode-
children子节点any-

EVENTS

名称描述参数
onCreated地图初始化完成事件-
onUpdateShow显示隐藏事件show: boolean

METHODS

名称描述定义