0

我正在尝试在下面实现一个 openlayers 热图示例:

https://openlayers.org/en/latest/examples/heatmap-earthquakes.html

是否可以显示数量而不是数量(同一区域中有多少点)?

编辑:指针计数并在缩小时显示:
缩小时指针计数和显示

4

1 回答 1

2

如果您的意思是将聚类特征显示为热图,则可以这样做。这是集群特征示例和热图示例的组合。

<!DOCTYPE html>
<html>
  <head>
    <title>Clustered Features</title>
    <link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css" type="text/css">
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
    <script src="https://openlayers.org/en/v4.6.5/build/ol.js"></script>
  </head>
  <body>
    <div id="map" class="map"></div>
    <form>
      <label>cluster distance</label>
      <input id="distance" type="range" min="0" max="100" step="1" value="40"/>
      <label>radius size</label>
      <input id="radius" type="range" min="1" max="50" step="1" value="5"/>
      <label>blur size</label>
      <input id="blur" type="range" min="1" max="50" step="1" value="15"/>
    </form>
    <script>
      var distance = document.getElementById('distance');
      var blur = document.getElementById('blur');
      var radius = document.getElementById('radius');

      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
      });

      var styleCache = {};
      var vector = new ol.layer.Heatmap({
        source: clusterSource,
        weight: function(feature) { return feature.get('features').length/1000; },
        blur: parseInt(blur.value, 10),
        radius: parseInt(radius.value, 10)
      });

      var raster = new ol.layer.Tile({
        source: new ol.source.OSM()
      });

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

      distance.addEventListener('input', function() {
        clusterSource.setDistance(parseInt(distance.value, 10));
      });

      blur.addEventListener('input', function() {
        vector.setBlur(parseInt(blur.value, 10));
      });

      radius.addEventListener('input', function() {
        vector.setRadius(parseInt(radius.value, 10));
      });

    </script>
  </body>
</html>

此版本显示缩放级别为 0、1 和 2 的普通集群图层以及缩放级别 3 或更高级别的热图

<!DOCTYPE html>
<html>
  <head>
    <title>Clustered Features</title>
    <link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css" type="text/css">
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
    <script src="https://openlayers.org/en/v4.6.5/build/ol.js"></script>
  </head>
  <body>
    <div id="map" class="map"></div>
    <form>
      <label>cluster distance</label>
      <input id="distance" type="range" min="0" max="100" step="1" value="60"/>
      <label>radius size</label>
      <input id="radius" type="range" min="1" max="50" step="1" value="40"/>
      <label>blur size</label>
      <input id="blur" type="range" min="1" max="50" step="1" value="15"/>
    </form>
    <script>
      var distance = document.getElementById('distance');
      var blur = document.getElementById('blur');
      var radius = document.getElementById('radius');

      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
      });

      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: 15,
                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;
        },
        minResolution: ol.tilegrid.createXYZ().getResolution(3) + 1
      });

      var vector = new ol.layer.Heatmap({
        source: clusterSource,
        weight: function(feature) { return feature.get('features').length/500; },
        blur: parseInt(blur.value, 10),
        radius: parseInt(radius.value, 10),
        maxResolution: ol.tilegrid.createXYZ().getResolution(3) + 1
      });

      var raster = new ol.layer.Tile({
        source: new ol.source.OSM()
      });

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

      distance.addEventListener('input', function() {
        clusterSource.setDistance(parseInt(distance.value, 10));
      });

      blur.addEventListener('input', function() {
        vector.setBlur(parseInt(blur.value, 10));
      });

      radius.addEventListener('input', function() {
        vector.setRadius(parseInt(radius.value, 10));
      });

    </script>
  </body>
</html>

如果您的数据是随机的,您可以找到当前最大的集群大小并在划分时使用它来计算权重:

    weight: function(feature) { 
       var maxSize = 0;
       clusterSource.forEachFeature( function(feature) { maxSize = Math.max(maxSize, feature.get('features').length); } );
       return feature.get('features').length/maxSize;
    },
于 2018-11-03T14:53:33.097 回答