openlayers 集群特征

import 'ol/ol.css';

import Feature from 'ol/Feature';

import Map from 'ol/Map';

import Point from 'ol/geom/Point';

import View from 'ol/View';

import { Circle as CircleStyle, Fill, Stroke, Style, Text, } from 'ol/style';

import {Cluster, OSM, Vector as VectorSource} from 'ol/source';

import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer';

import {boundingExtent} from 'ol/extent';

const count = 20000;
const features = new Array(count);
const e = 4500000;

// 设置随机坐标点
for (let i = 0; i < count; ++i) {
  const coordinates = [2 * e * Math.random() - e, 2 * e * Math.random() - e];
  features[i] = new Feature(new Point(coordinates));
}


const source = new VectorSource({
  features: features,
});

const clusterSource = new Cluster({
  distance: parseInt(distanceInput.value, 10),
  minDistance: parseInt(minDistanceInput.value, 10),
  source: source,
});

   this.clusters = new VectorLayer({

      source: this.clusterSource,

      style: (feature) => {

        const size = feature.get('features').length;

        if (size === 1) {

                // 当缩放到最小层级是可变换为自定义坐标点

                return new Style()

        } else {

          return new Style({

            image: new CircleStyle({

              radius: 10,

              stroke: new Stroke({

                color: '#fff'

              }),

              fill: new Fill({

                color: '#3399CC'

              })

            }),

            text: new Text({

              text: size.toString(),

              fill: new Fill({

                color: '#fff'

              })

            })

          });

        }

      }

    });

// 加载默认图层

const raster = new TileLayer({
  source: new OSM(),
});
// 初始化地图
const map = new Map({
  layers: [raster, clusters],
  target: 'map',
  view: new View({
    center: [0, 0],
    zoom: 2,
  }),
});

// 海量坐标点的点击事件

map.on('click', (e) => {
  clusters.getFeatures(e.pixel).then((clickedFeatures) => {
    if (clickedFeatures.length) {
      // Get clustered Coordinates
      const features = clickedFeatures[0].get('features');
      if (features.length > 1) {
        const extent = boundingExtent(
          features.map((r) => r.getGeometry().getCoordinates())
        );
        map.getView().fit(extent, {duration: 1000, padding: [50, 50, 50, 50]});
      } else {

        // 点击最小层级的标点,可实现自己的业务

        }
    }
  });
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值