2.7.1 • Published 4 months ago

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

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

GroundOverlay 地面叠加层

Buy me a coffee npm version Downloads

地图上的地面叠加层。

import { GroundOverlay, useGroundOverlay } from '@uiw/react-baidu-map';
// 或者单独安装使用
import GroundOverlay, { useGroundOverlay } from '@uiw/react-baidu-map-ground-overlay';

基本用法

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

const Example = () => {
  const [visiable, setVisiable] = useState(true);
  const bounds = new BMap.Bounds(
    new BMap.Point(116.29579,39.837146),
    new BMap.Point(116.475451,39.9764)
  );
  function groundRef(props) {
    if (props && props.groundOverlay) {
      console.log('groundOverlay:', props.groundOverlay);
    }
  }
  return (
    <>
      <button onClick={() => setVisiable(!visiable)}>{visiable ? '隐藏' : '显示'}</button>
      <Map center="北京" zoom={11} widget={['NavigationControl']} style={{ height: 350 }}>
        {visiable && (
          <GroundOverlay
            ref={groundRef}
            bounds={bounds}
            imageURL="http://lbsyun.baidu.com/jsdemo/img/si-huan.png"
            opacity={1}
            displayOnMinLevel={10}
            displayOnMaxLevel={14}
          />
        )}
      </Map>
    </>
  );
}

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

export default Demo;

使用 hooks

groundOverlay, useGroundOverlay

⚠️ 不知为何 showhide 方法存在但不起作用,理论上是继承 Overlay 类就会有 show()hide() 方法。

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

const Example = () => {
  const divElm = useRef(null);
  const [visiable, setVisiable] = useState(true);
  const bounds = new BMap.Bounds(
    new BMap.Point(116.29579,39.837146),
    new BMap.Point(116.475451,39.9764)
  );
  const { map, setContainer } = useMap({ center: '北京', zoom: 11, widget: ['GeolocationControl', 'NavigationControl'] });
  const { groundOverlay, setGroundOverlay } = useGroundOverlay({ map, bounds, visiable, imageURL: 'http://lbsyun.baidu.com/jsdemo/img/si-huan.png',
    onClick: () => {
      console.log('点击事件!');
    },
  });

  useEffect(() => {
    if (divElm.current) {
      setContainer(divElm.current);
    }
  });
  useEffect(() => {
    if (groundOverlay && map) {
      // 不知为何 方法存在但不起作用,
      // 理论上是继承 Overlay 类就会有 `show` 和 `hide` 方法。
      console.log('groundOverlay:hide:', visiable, groundOverlay.hide);
      console.log('groundOverlay:show:', visiable, groundOverlay.show);
      visiable ? groundOverlay.hide() : groundOverlay.show();
    }
  }, [visiable]);
  return (
    <>
      <button onClick={() => setVisiable(!visiable)}>{visiable ? '隐藏' : '显示'}</button>
      <div ref={divElm} style={{ height: '100%' }} />
    </>
  )
}

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

export default Demo;

Props

参数说明类型默认值
bounds必填 图层显示的矩形区域BMap.Bounds(sw: Point, ne: Point)-
imageURL必填 图层地址string-
opacity图层透明度number-
displayOnMinLevel图层显示的最小级别number-
displayOnMaxLevel图层显示的最小级别number-
const bounds = new BMap.Bounds(
  new BMap.Point(116.29579,39.837146),
  new BMap.Point(116.475451,39.9764)
);

事件

参数说明类型默认值
onClick鼠标点击图层后会触发此事件(event: { type: string, target: any }) => void;-
onDblClick鼠标双击图层后会触发此事件(event: { type: string, target: any }) => void;-
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