2.7.1 • Published 4 months ago

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

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

Label 文本标注

Buy me a coffee npm version Downloads

表示地图上包 表示地图上包含信息的窗口。

import { Label, useLabel } from '@uiw/react-baidu-map';
// 或者单独安装使用
import Label, { useLabel } from '@uiw/react-baidu-map-label';

基本用法

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

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

const Example = () => {
  const [visiable, setVisiable] = useState(true);
  const [content, setContent] = useState('上海市 <del>青浦区</del> 徐泾镇盈港东路');
  function labelRef(props) {
    if (props && props.label) {
      console.log('label:', props.label, props.map, props.BMap);
    }
  }
  return (
    <>
      <input value={content} onChange={(e) => setContent(e.target.value)} />
      <button onClick={() => setVisiable(!visiable)}>{visiable ? '隐藏' : '显示'}</button>
      <Map zoom={13} widget={['NavigationControl']} style={{ height: 350 }} >
        <Label visiable={visiable} content={content} position={{ lng: 121.436256, lat: 31.246926 }} />
        <Label
          ref={labelRef}
          title="wwww"
          content={content}
          position={{ lng: 121.501365, lat: 31.224942 }}
          onClick={() => setContent('您!点击了文本标注!')}
        />
        <div>
          <Label content="test" visiable={visiable} title="wwww" position={{ lng: 121.501365, lat: 31.234942 }}>
            children - 2<br /> 上海市 <del>青浦区</del> 徐泾镇盈港东路
          </Label>
        </div>
      </Map>
    </>
  );
}

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

使用 hooks

label, setLabel

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

const Example = () => {
  const [zoom, setZoom] = useState(15)
  const divElm = useRef(null);
  const { map } = useMapContext();
  const { label, Portal } = useLabel({
    content: '徐泾镇',
    position: { lng: 121.501365, lat: 31.224942 },
    onClick: () => {
      console.log('点击事件!');
    }
  });

  useEffect(() => {
    if (map) {
      // 启用滚轮放大缩小,默认禁用
      map.setZoom(zoom);
    }
  }, [zoom, map]);
  
  let counts = zoom || 15;
  return (
    <>
      <button onClick={() => setZoom(counts-1)}>-</button>
      <span>{zoom || 10}</span>
      <button onClick={() => setZoom(counts+1)}>+</button>
      <Portal><div>xxxxxx</div></Portal>
    </>
  )
}

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

使用 Provider

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

const Example = () => {
  const [zoom, setZoom] = useState(15)
  const divElm = useRef(null);
  const { map, setContainer } = useMap({
    zoom: 9,
    widget: ['GeolocationControl', 'NavigationControl']
  });
  const { label } = useLabel({ map,
    content: '徐泾镇',
    position: { lng: 121.436256, lat: 31.246926 },
    onClick: () => {
      console.log('点击事件!');
    }
  });

  useEffect(() => {
    if (map) {
      // 启用滚轮放大缩小,默认禁用
      map.setZoom(zoom);
    }
  }, [zoom, map]);

  useEffect(() => {
    if (divElm.current) {
      setContainer(divElm.current);
    }
  }, [divElm.current]);
  let counts = zoom || 15;
  return (
    <>
      <button onClick={() => setZoom(counts-1)}>-</button>
      <span>{zoom || 15}</span>
      <button onClick={() => setZoom(counts+1)}>+</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

参数说明类型默认值
content文本标注内容string-
children展示文本标注内容,🚧 添加 children 的时候 content 将失效JSX.Element-
offset文本标注的位置偏移值Size-
position文本标注的地理位置Point-
title文本标注的标题string-
zIndex设置覆盖物的 zIndexnumber-
style文本标注样式,该样式将作用于文本标注的容器元素上,如:{ color : "red", fontSize : "12px" }Style-
visiable覆盖物是否可见。boolean-
enableMassClear允许覆盖物在 map.clearOverlays 方法中被清除boolean-

事件

参数说明类型默认值
onClick点击文本标注后会触发此事件(event: { type: string, target: any }) => void;-
onDblClick双击文本标注后会触发此事件(event: { type: string, target: any }) => void;-
onMouseDown鼠标在文本标注上按下触发此事件(event: { type: string, target: any }) => void;-
onMouseUp鼠标在文本标注释放触发此事件(event: { type: string, target: any }) => void;-
onMouseOout鼠标离开文本标注时触发此事件(event: { type: string, target: any }) => void;-
onMouseOver当鼠标进入文本标注区域时会触发此事件(event: { type: string, target: any }) => void;-
onRemove移除文本标注时触发(event: { type: string, target: any }) => void;-
onRightClick右键点击标注时触发此事件(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