Skip to content

MapPopup

Example

Example Source Code
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

NameDescriptionTypeDefault
anchorSpecifies which part of the marker is closest to the coordinate pointstring('center', 'top', 'bottom', 'left', 'right', 'top-left', 'top-right', 'bottom-left', 'bottom-right')-
classNameClass added to the popup DOM elementstring-
coordinatesThe longitude and latitude position of the markernumber[]-
hideOnClickWhether the popup automatically closes when the map is clickedbooleanfalse
hideOnMoveWhether the popup automatically closes when the mouse moves on the mapbooleanfalse
offsetThe popup offset. Positive values for right/downnumber / number[][0,0]
showv-model Whether the popup is visiblebooleantrue
showTipWhether to automatically add a triangular tip to the popupbooleantrue
tipColorThe color of the triangular tipstring#fff
contentCustom popup content nodeReactNode-
childrenChildren nodesany-

EVENTS

NameDescriptionParameters
onCreatedMap initialization completed event-
onUpdateShowShow/Hide eventshow: boolean

METHODS

NameDescriptionDefinition