2.7.1 • Published 4 months ago

@uiw/react-baidu-map-curve-line v2.7.1

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

CurveLine 弧线组件

Buy me a coffee npm version Downloads

使用浏览器的矢量制图工具(如果可用)在地图上绘制弧线的地图叠加层,当前组件自动加载 CurveLine.min.js 包,加载完成将会有个 window.BMapLib 的全局对象。

import { CurveLine, useCurveLine } from '@uiw/react-baidu-map';
// 或者单独安装使用
import CurveLine, { useCurveLine } from '@uiw/react-baidu-map-curve-line';

基本用法

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

const Example = () => {
  const [enableEditing, setEnableEditing] = useState(false);
  const [strokeOpacity, setStrokeOpacity] = useState(0.5);
  function curveLineRef(props) {
    if (props && props.curveLine) {
      console.log('curveLine:', props.curveLine, props.map, props.BMapLib);
    }
  }
  return (
    <>
      <button onClick={() => setEnableEditing(!enableEditing)}>{enableEditing ? '取消编辑' : '编辑'}</button>
      <button onClick={() => setStrokeOpacity(0.7)}>透明度0.7</button>
      <button onClick={() => setStrokeOpacity(0.2)}>透明度0.2</button>
      <Map zoom={4} center="武汉" widget={['NavigationControl']} style={{ height: 350 }}>
        <CurveLine
          ref={curveLineRef}
          enableEditing={enableEditing}
          strokeOpacity={strokeOpacity}
          strokeWeight={3}
          strokeColor="blue"
          path={[
            { lng: 116.432045, lat: 39.910683 },
            { lng: 114.300404, lat: 30.63216 },
            { lng: 121.491121, lat: 25.127053 },
          ]}
        />
      </Map>
    </>
  );
}

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

使用 hooks

curveLine, setCurveLine, path, setPath, BMapLib

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

const Example = () => {
  const [enableEditing, setEnableEditing] = useState(false);
  const [strokeOpacity, setStrokeOpacity] = useState(0.9);
  const divElm = useRef(null);
  const { setContainer, map } = useMap({
    zoom: 8,
    enableScrollWheelZoom: true,
    widget: ['GeolocationControl', 'NavigationControl']
  });
  const { curveLine } = useCurveLine({ map,
    enableEditing, strokeOpacity,
    path: [
      { lng: 118.798544, lat: 32.076761 },
      { lng: 120.785452, lat: 30.759355 },
      { lng: 120.895835, lat: 31.974881 },
    ],
  });
  useCurveLine({ map,
    path: [
      { lng: 118.770948, lat: 30.9419 },
      { lng: 120.583081, lat: 31.313834 },
      { lng: 121.484549, lat: 31.226918 },
    ],
  });

  useEffect(() => {
    if (divElm.current) {
      setContainer(divElm.current);
    }
  });

  useEffect(() => {
    if (map && curveLine) {
      if (enableEditing) {
        curveLine.enableEditing();
      } else {
        curveLine.disableEditing();
      }
    }
  }, [enableEditing]);

  useEffect(() => {
    if (map && curveLine) {
      curveLine.setStrokeOpacity(strokeOpacity);
    }
  }, [strokeOpacity]);

  return (
    <>
      <button onClick={() => setEnableEditing(!enableEditing)}>{enableEditing ? '取消编辑' : '编辑'}</button>
      <button onClick={() => setStrokeOpacity(0.7)}>透明度0.7</button>
      <button onClick={() => setStrokeOpacity(0.2)}>透明度0.2</button>
      <div ref={divElm} style={{ height: 350 }} />
    </>
  )
}

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

Props

参数说明类型默认值
visiable覆盖物是否可见。boolean-
strokeColor折线颜色String-
strokeWeight折线的宽度,以像素为单位Number-
strokeOpacity折线的透明度,取值范围0 - 1Number-
strokeStyle折线的样式,solid或dashedString-
enableMassClear是否在调用map.clearOverlays清除此覆盖物,默认为trueBoolean-
enableEditing是否启用线编辑,默认为falseBooleanfalse
enableClicking是否响应点击事件,默认为trueBoolean-
icons配置贴合折线的图标IconSequence[]-

事件

参数说明类型
onClick点击折线后会触发此事件(event: { type: string, target: any, point: Point, pixel: Pixel }): void;
onDblClick双击折线后会触发此事件(event: { type: string, target: any, point: Point, pixel: Pixel }): void;
onMouseDown鼠标在折线上按下触发此事件(event: { type: string, target: any, point: Point, pixel: Pixel }): void;
onMouseUp鼠标在折线释放触发此事件(event: { type: string, target: any, point: Point, pixel: Pixel }): void;
onMouseOut鼠标离开折线时触发此事件(event: { type: string, target: any, point: Point, pixel: Pixel }): void;
onMouseOver当鼠标进入折线区域时会触发此事件(event: { type: string, target: any, point: Point, pixel: Pixel }): void;
onRemove移除折线时触发(event: { type: string, target: any }): void;
onLineUpdate覆盖物的属性发生变化时触发(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