2.7.1 • Published 4 months ago

@uiw/react-baidu-map-point-collection v2.7.1

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

PointCollection 加载海量点

Buy me a coffee npm version Downloads

表示海量点类,利用该类可同时在地图上展示万级别的点,目前仅适用于html5浏览器。

渲染海量点会耗费一定性能,应该注意不要频繁变动 point 数组

import { PointCollection, usePointCollection } from '@uiw/react-baidu-map';
// 或者单独安装使用
import PointCollection, { usePointCollection } from '@uiw/react-baidu-map-point-collection';

基本用法

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

const Example = () => {
  const [visiable, setVisiable] = useState(true);
  const [points, setPoints] = useState([]);
  const [position, setPositon] = useState('');
  useEffect(() => {
    if(points.length === 0) {
      requireScript('https://lbsyun.baidu.com/jsdemo/data/points-sample-data.js').then(() => {
        if(window.data && window.data.data) {
          setPoints(window.data.data);
        }
      });
    }
  });
  function compRef(props) {
    if (props && props.pointCollection) {
      console.log('pointCollection::', props.pointCollection, props.map, props.BMap);
    }
  }
  return (
    <>
      <button onClick={() => setVisiable(!visiable)}>{visiable ? '隐藏' : '显示'}</button>
      {position && <span>标注点经纬度:{position}</span>}
      <Map widget={['NavigationControl']} zoom={5} style={{ height: 350 }}>
        <PointCollection
          ref={compRef}
          visiable={visiable}
          onClick={(e) => {
            setPositon(JSON.stringify(e.point))
          }}
          styles={{ shape: 1 }}
          points={[
            ...points
          ]}
        />
      </Map>
    </>
  )
}

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

使用 hooks

可以使用 pointCollection, setPointCollection, points, setPoints 钩子函数。

下面实例来源官方网站实例

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

let loadData = false;
let data = []
const Example = () => {
  const divElm = useRef();
  const { setContainer, map } = useMap({ widget: ['GeolocationControl', 'NavigationControl'], zoom: 5 });
  const { points, setPoints } = usePointCollection({ map });
  useEffect(() => {
    if (divElm.current && !map) {
      setContainer(divElm.current);
    }
    if (!loadData) {
      requireScript('https://lbsyun.baidu.com/jsdemo/data/points-sample-data.js').then(() => {
        if(window.data && window.data.data) {
          if (!loadData) {
            loadData = true;
            data = window.data.data;
            setPoints(window.data.data);
          }
        }
      });
    }
  });
  return (
    <>
      <button onClick={() => setPoints(points.length > 0 ? [] : data)}>{points && points.length > 0 ? '清空标注' : '显示标注'}</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

参数说明类型默认值
points必填 创建海量点类。为点的坐标集合 [[74.438,39.006,1],[74.932,38.382,1]]lng: number, lat: number-
visiable覆盖物是否可见。boolean-
styles设置 {shape,size,color},是一个可控属性object-
shape海量点的预设形状ShapeTypeBMAP_POINT_SHAPE_WATERDROP
size海量点的预设尺寸SizeTypeBMAP_POINT_SIZE_SMALL
color海量点的颜色,默认为'#fa937e',同时支持颜色字符串,如'red';string#d340c3

事件

参数说明类型
onClick鼠标点击点时会触发此事件(event: { type: string, target: any, point: Point }) => void;
onMouseOver鼠标移入点时会触发该事件(event: { type: string, target: any, point: Point }) => void;
onMouseOut鼠标移出点时会触发该事件(event: { type: string, target: any }) => void;

ShapeType

常量描述
BMAP_POINT_SHAPE_CIRCLE圆形,为默认形状
BMAP_POINT_SHAPE_STAR星形
BMAP_POINT_SHAPE_SQUARE方形
BMAP_POINT_SHAPE_RHOMBUS菱形
BMAP_POINT_SHAPE_WATERDROP水滴状,该类型无size和color属性

SizeType

常量描述
BMAP_POINT_SIZE_TINY定义点的尺寸为超小,宽高为2px*2px
BMAP_POINT_SIZE_SMALLER定义点的尺寸为很小,宽高为4px*4px
BMAP_POINT_SIZE_SMALL定义点的尺寸为小,宽高为8px*8px
BMAP_POINT_SIZE_NORMAL定义点的尺寸为正常,宽高为10px*10px,为海量点默认尺寸
BMAP_POINT_SIZE_BIG定义点的尺寸为大,宽高为16px*16px
BMAP_POINT_SIZE_BIGGER定义点的尺寸为很大,宽高为20px*20px
BMAP_POINT_SIZE_HUGE定义点的尺寸为超大,宽高为30px*30px
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