openlayers3 聚合分析专题图 cluster

openlayers3 聚合分析专题图 cluster 

  在地图上查询结果通常以标记点的形式展现,但是如果标记点较多,不仅会大大增加客户端的渲染时间,让客户端变得很卡,而且会让人产生密集恐惧症,为了解决这一问题,我们需要一种手段能在用户有限的可视区域范围内,利用最小的区域展示出最全面的信息,而又不产生重叠覆盖。具体算法探讨:点击打开链接

openlayers3提供api具体实例使用方法:

  一.简单实例:点击打开链接

1.创建feature,添加到ol.source.Cluster 中;设置聚合半径distance红色标注

var count = 20000;
      var features = new Array(count);
      var e = 4500000;
      for (var i = 0; i < count; ++i) {
        var coordinates = [2 * e * Math.random() - e, 2 * e * Math.random() - e];
        features[i] = new ol.Feature(new ol.geom.Point(coordinates));
      }

      var source = new ol.source.Vector({
        features: features
      });

      var clusterSource = new ol.source.Cluster({
        distance: parseInt(distance.value, 10),
        source: source
      });

2.添加到ol.layer.Vector图层中,并根据feature属性设置对应style

var styleCache = {};
      var clusters = new ol.layer.Vector({
        source: clusterSource,
        style: function(feature) {
          var size = feature.get('features').length;
          var style = styleCache[size];
          if (!style) {
            style = new ol.style.Style({
              image: new ol.style.Circle({
                radius: 10,
                stroke: new ol.style.Stroke({
                  color: '#fff'
                }),
                fill: new ol.style.Fill({
                  color: '#3399CC'
                })
              }),
              text: new ol.style.Text({
                text: size.toString(),
                fill: new ol.style.Fill({
                  color: '#fff'
                })
              })
            });
            styleCache[size] = style;
          }
          return style;
        }
      });

3.添加到map中即可

 var map = new ol.Map({
        layers: [raster, clusters],
        target: 'map',
        view: new ol.View({
          center: [0, 0],
          zoom: 2
        })
      });

二.实际应用 EarthQuake

1.初始化聚合图层,设置distance,绑定缩放事件

 vector = new ol.layer.Vector({
        source: new ol.source.Cluster({
          distance: 40,
          source: new ol.source.Vector({
            url: 'https://openlayers.org/en/v4.2.0/examples/data/kml/2012_Earthquakes_Mag5.kml',
            format: new ol.format.KML({
              extractStyles: false
            })
          })
        }),
        style: styleFunction
      });
 

2.根据当前缩放resolution,及feature属性设置渲染style

var currentResolution;
      function styleFunction(feature, resolution) {
        if (resolution != currentResolution) {
          calculateClusterInfo(resolution);
          currentResolution = resolution;
        }
        var style;
        var size = feature.get('features').length;
        if (size > 1) {
          style = new ol.style.Style({
            image: new ol.style.Circle({
              radius: feature.get('radius'),
              fill: new ol.style.Fill({
                color: [255, 153, 0, Math.min(0.8, 0.4 + (size / maxFeatureCount))]
              })
            }),
            text: new ol.style.Text({
              text: size.toString(),
              fill: textFill,
              stroke: textStroke
            })
          });
        } else {
          var originalFeature = feature.get('features')[0];
          style = createEarthquakeStyle(originalFeature);
        }
        return style;
      }

3.根据点击聚合图形,设置渲染style

function selectStyleFunction(feature) {
        var styles = [new ol.style.Style({
          image: new ol.style.Circle({
            radius: feature.get('radius'),
            fill: invisibleFill
          })
        })];
        var originalFeatures = feature.get('features');
        var originalFeature;
        for (var i = originalFeatures.length - 1; i >= 0; --i) {
          originalFeature = originalFeatures[i];
          styles.push(createEarthquakeStyle(originalFeature));
        }
        return styles;
      }
  1. 4.添加聚合图层,并绑定点击事件
 var map = new ol.Map({
        layers: [raster, vector],
        interactions: ol.interaction.defaults().extend([new ol.interaction.Select({
          condition: function(evt) {
            return evt.originalEvent.type == 'mousemove' ||
                evt.type == 'singleclick';
          },
          style: selectStyleFunction
        })]),
        target: 'map',
        view: new ol.View({
          center: [0, 0],
          zoom: 2
        })
      });

You may also like...

发表评论

您的电子邮箱地址不会被公开。

CAPTCHAis initialing...