2.7.1 • Published 4 months ago

@uiw/react-baidu-map-control v2.7.1

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

Control 自定义控件

Buy me a coffee npm version Downloads

您可以通过此组件来实现自定义控件。同时你可以使用 Map 组件 widget 属性来更方便,设置自带默认控件。

import { Control, useControl } from '@uiw/react-baidu-map';
// 或者单独安装使用
import Control, { useControl } from '@uiw/react-baidu-map-control';

基本用法

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

const Example = () => {
  const [count, setCount] = useState(0);
  const map = useRef(null);
  return (
    <Map zoom={13} ref={map} widget={['NavigationControl']} style={{ height: 350 }}>
      <Control
        ref={(instance) => {
          if (instance && instance.control) {
            instance.control.setAnchor(BMAP_ANCHOR_BOTTOM_RIGHT);
          }
        }}
      >
        <div
          style={{ background: 'gray', padding: '10px', fontSize: 12 }}
          onClick={() => setCount(count + 1)}
        >
          Current Count: {count}
        </div>
      </Control>
      <Control anchor={BMAP_ANCHOR_TOP_RIGHT}>
        <div
          style={{ background: 'gray', padding: '10px', fontSize: 12, display: 'inline-block' }}
          onClick={() => {
            const instanceMap = map.current.map
            if (instanceMap) {
              instanceMap.setZoom(instanceMap.getZoom() + 2);
            }
          }}
        >
          放大2级
        </div>
        <div
          style={{ background: 'gray', padding: '10px', fontSize: 12, display: 'inline-block', color: '#fff' }}
          onClick={() => {
            const instanceMap = map.current.map
            if (instanceMap) {
              instanceMap.setZoom(instanceMap.getZoom() - 2);
            }
          }}
        >
          缩小2级
        </div>
      </Control>
    </Map>
  );
}

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

export default Demo;

使用 hooks

control, setControl

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

const Example = () => {
  const divElm = useRef(null);
  const [count, setCount] = useState(4);
  const { map } = useMapContext();
  const { ControlPortal } = useControl({ anchor: BMAP_ANCHOR_TOP_RIGHT });
  const { ControlPortal: ControlPortal2 } = useControl({ anchor: BMAP_ANCHOR_BOTTOM_RIGHT });
  return (
    <>
      <ControlPortal>
        <div style={{ background: 'gray', padding: '10px' }}>
          <button
            onClick={() => {
              setCount(count + 1);
              map.setZoom(map.getZoom() + 1);
            }}
          >
            放大1级 {count}
          </button>
          <button
            onClick={() => {
              setCount(count - 1);
              map.setZoom(map.getZoom() - 2);
            }}
          >
            缩小1级 {count}
          </button>
        </div>
      </ControlPortal>
      <ControlPortal2>
        <div
          style={{ background: 'gray', padding: '10px', fontSize: 12 }}
          onClick={() => setCount(count + 1)}
        >
          Current Count: {count}
        </div>
      </ControlPortal2>
    </>
  )
}

const Demo = () => (
  <div style={{ width: '100%' }}>
    <APILoader akay="eYpCTECSntZmw0WyoQ7zFpCRR9cpgHFG">
      <Provider>
        <Map zoom={13} widget={['NavigationControl']} style={{ height: 350 }}>
          <Example />
        </Map>
      </Provider>
    </APILoader>
  </div>
);

export default Demo;

Props

参数说明类型默认值
visiable覆盖物是否可见。boolean-
anchor控件的停靠位置。ControlAnchorBMAP_ANCHOR_TOP_LEFT
offset控件的位置偏移值。BMap.Sizenew BMap.Size(10, 10)
children展示控件内容。JSX.Element-

ControlAnchor

此常量表示控件的定位。

常量描述
BMAP_ANCHOR_TOP_LEFT0 控件将定位到地图的左上角
BMAP_ANCHOR_TOP_RIGHT1 控件将定位到地图的右上角
BMAP_ANCHOR_BOTTOM_LEFT2 控件将定位到地图的左下角
BMAP_ANCHOR_BOTTOM_RIGHT3 控件将定位到地图的右下角
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