uiwjs / react-baidu-map

基于 React 封装的百度地图组件,支持 React Hook,帮助你轻松的接入地图到 React 项目中。
https://uiwjs.github.io/react-baidu-map
MIT License
225 stars 22 forks source link

你好,点聚合的案例有吗 #163

Closed xiangjiayu closed 3 years ago

jaywcjlove commented 3 years ago

@xiangjiayu 参照官方示例,来写哦, API都弄出来了。

xiangjiayu commented 3 years ago

ok,我按官方实例写出来了

jaywcjlove commented 3 years ago

@xiangjiayu 欢迎分享你的示例。

xiangjiayu commented 3 years ago

官方文档的实例就是把marker 集合成数组 。调用即可。new BMapLib.MarkerClusterer(map, { markers });

import React, { useState, useEffect, useRef } from 'react';
import { connect, Dispatch } from 'umi';
import { APILoader, useMap, useMarker, useInfoWindow, useLabel, useCircle } from '@uiw/react-baidu-map';
import { isEmpty } from 'lodash';
import { GridContent } from '@ant-design/pro-layout';
import { Spin } from 'antd';
import styles from '../AuntDistribution/index.less'

interface AuntPolymerizeProps {
  dispatch: Dispatch;
}

const AuntPolymerize: React.FC<AuntPolymerizeProps> = (props) => {
  const { dispatch } = props;

  const [center, setCenter] = useState<string | BMap.Point>('南京市');
  const [point, setPoint] = useState<any>([]);

  const Example = () => {
    const divElm = useRef<any>(null);

    const onLoad = (e: any) => {
      if (e && e.target) {
        if (!isEmpty(point)) {
          const markers: any = [];
          point.forEach((item: any) => {
            const pt = new BMap.Point(item.longitude, item.latitude);
            const ptMarker = new BMap.Marker(pt);
            markers.push(ptMarker);
            // 标点添加点击事件
            orderMarker.addEventListener('click', () => {});
          });
          // 点聚合
          new BMapLib.MarkerClusterer(map, { markers });
        }
      }
    };
    const { map, setContainer } = useMap({
      center,
      enableMapClick:true,
      zoom:16,
      enableScrollWheelZoom:true,
      onLoad,
    });

    useEffect(() => {
      if (divElm.current) {
        setContainer(divElm.current);
      }
    });
    return <div ref={divElm} style={{ height: '100%' }} />
  };

  return <div className={styles.mapWrap}>
        <APILoader>
          <Example />
        </APILoader>
      </div>
};

export default connect()(AuntPolymerize);