2.7.1 • Published 4 months ago

@uiw/react-baidu-map-custom-overlay v2.7.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

CustomOverlay 自定义覆盖物

Buy me a coffee npm version Downloads

用于渲染自定义的 DOM 对象。

import { CustomOverlay, useCustomOverlay } from '@uiw/react-baidu-map';
// 或者单独安装使用
import CustomOverlay, { useCustomOverlay } from '@uiw/react-baidu-map-custom-overlay';

基本用法

官方原生 JS 实例:https://lbsyun.baidu.com/jsdemo.htm#c1_11

import React, { useState } from 'react';
import { APILoader, Map, Marker, CustomOverlay } from '@uiw/react-baidu-map';

const Demo = () => {
  const [count, setCount] = useState(0);
  function markerRef(props) {
    if (props && props.customOverlay) {
      console.log('CustomOverlay::', props.customOverlay, props.map, props.BMap);
    }
  }
  function handleClick(event) {
    event.stopPropagation();
    event.preventDefault();
    setCount(count + 1);
  }
  return (
    <div style={{ width: '100%' }}>
      <APILoader akay="eYpCTECSntZmw0WyoQ7zFpCRR9cpgHFG">
        <Map widget={['NavigationControl']} zoom={13} style={{ height: 350 }}>
          <Marker position={{ lng: 121.466008, lat: 31.220001 }} />
          <CustomOverlay
            ref={markerRef}
            paneName="floatPane"
            position={{ lng: 121.466008, lat: 31.220001 }} 
            style={{
              whiteSpace: 'nowrap',
              // transform: `translateX(-50%)`,
            }}
          >
            <div
              style={{ backgroundColor: '#fff', padding: 5, whiteSpace: 'nowrap', transform: `translateX(-50%)` }}
            >
              <button onClick={handleClick}>点击递增{count}</button>
            </div>
          </CustomOverlay>
          <CustomOverlay position={{ lng: 121.500934, lat: 31.229882 }} zIndex={99}>
            <div style={{ backgroundColor: '#fff', padding: 5, borderRadius: 10, whiteSpace: 'nowrap', border: '1px solid #333', userSelect: 'initial' }}>
              自定义的覆盖物
            </div>
          </CustomOverlay>
          <CustomOverlay position={{ lng: 121.500934, lat: 31.23088 }}>
            <div style={{ backgroundColor: '#fff', padding: 5, borderRadius: 10, whiteSpace: 'nowrap', border: '1px solid #333' }}>
              自定义的覆盖物,第二个
            </div>
          </CustomOverlay>
        </Map>
      </APILoader>
    </div>
  );
};
export default Demo;

使用 hooks

portal, setPortal, customOverlay, setCustomOverlay

import React from 'react';
import { useRef, useEffect, useState } from 'react';
import { Map, APILoader, Provider, useMap, useMarker, useCustomOverlay } from '@uiw/react-baidu-map';

const Example = () => {
  const divElm = useRef();
  const [count, setCount] = useState(0);
  const { setContainer, map } = useMap({
    zoom: 13,
    enableScrollWheelZoom: true,
    widget: ['GeolocationControl', 'NavigationControl']
  });
  useMarker({ map, position: { lng: 121.466008, lat: 31.220001 } });

  function handleClick(event) {
    event.stopPropagation();
    event.preventDefault();
    setCount(count + 1);
  }
  const { portal } = useCustomOverlay({
    map,
    position: { lng: 121.466008, lat: 31.220001 },
    children: (
      <div style={{ backgroundColor: '#fff', padding: 5, whiteSpace: 'nowrap', transform: `translateX(-50%)` }}>
        <button onClick={handleClick}>自定义的覆盖物{count}</button>
      </div>
    ),
  });
  useEffect(() => {
    if (divElm.current && !map) {
      setContainer(divElm.current);
    }
  });

  return (
    <>
      <div ref={divElm} style={{ height: 350 }} />
      {portal}
    </>
  )
}

const Demo = () => (
  <div style={{ width: '100%' }}>
    <APILoader akay="eYpCTECSntZmw0WyoQ7zFpCRR9cpgHFG">
      <Provider>
        <Example />
      </Provider>
    </APILoader>
  </div>
);
export default Demo;

Props

参数说明类型默认值
children自定义 DOM 元素。ReactNode-
position必填 设置标注的地理坐标。百度拾取坐标系统Point-
visiable覆盖物是否可见。boolean-
zIndex对应 css z-index 属性,当添加了多个 CustomOverlay 时,可以用于设置层叠顺序number-
paneName自定义覆盖物插入的容器,文档描述floatPane, markerMouseTarget, floatShadow, labelPane, markerPane, markerShadow, mapPanemarkerPane
2.7.1

4 months ago

2.7.0

5 months ago

2.6.3

5 months ago

2.6.2

9 months ago

2.6.1

12 months ago

2.6.0

1 year ago

2.5.5

1 year ago

2.5.4

1 year ago

2.5.3

2 years ago

2.5.0

2 years ago

2.5.2

2 years ago

2.5.1

2 years ago

2.3.0

2 years ago

2.1.2

2 years ago

2.2.0

2 years ago

2.1.1

2 years ago

2.4.0

2 years ago

2.0.7

2 years ago

2.0.8

2 years ago

2.1.0

2 years ago

2.0.6

2 years ago

2.0.5

3 years ago

2.0.3

3 years ago

2.0.4

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago