• 鼠标移上去扩散的聚合


    <!DOCTYPE html>
    <html>
      <head>
        <title>Earthquake Clusters</title>
        <link rel="stylesheet" href="http://openlayers.org/en/v3.13.0/css/ol.css" type="text/css">
        <script src="http://openlayers.org/en/v3.13.0/build/ol.js"></script>
        <style>
          #map {
            position: relative;
          }
          #info {
            position: absolute;
            height: 1px;
            width: 1px;
            z-index: 100;
          }
          .tooltip.in {
            opacity: 1;
            filter: alpha(opacity=100);
          }
          .tooltip.top .tooltip-arrow {
            border-top-color: white;
          }
          .tooltip-inner {
            border: 2px solid white;
          }
        </style>
      </head>
      <body>
        <div id="map" class="map"></div>
        <script>
          var earthquakeFill = new ol.style.Fill({
            color: 'rgba(255, 153, 0, 0.8)'
          });
          var earthquakeStroke = new ol.style.Stroke({
            color: 'rgba(255, 204, 0, 0.2)',
             1
          });
          var textFill = new ol.style.Fill({
            color: '#fff'
          });
          var textStroke = new ol.style.Stroke({
            color: 'rgba(0, 0, 0, 0.6)',
             3
          });
          var invisibleFill = new ol.style.Fill({
            color: 'rgba(255, 255, 255, 0.01)'
          });
    
          function createEarthquakeStyle(feature) {
            // 2012_Earthquakes_Mag5.kml stores the magnitude of each earthquake in a
            // standards-violating <magnitude> tag in each Placemark.  We extract it
            // from the Placemark's name instead.
            var name = feature.get('name');
            var magnitude = parseFloat(name.substr(2));
            var radius = 5 + 20 * (magnitude - 5);
    
            return new ol.style.Style({
              geometry: feature.getGeometry(),
              image: new ol.style.RegularShape({
                radius1: radius,
                radius2: 3,
                points: 5,
                angle: Math.PI,
                fill: earthquakeFill,
                stroke: earthquakeStroke
              })
            });
          }
    
          var maxFeatureCount, vector;
          function calculateClusterInfo(resolution) {
            maxFeatureCount = 0;
            var features = vector.getSource().getFeatures();
            var feature, radius;
            for (var i = features.length - 1; i >= 0; --i) {
              feature = features[i];
              var originalFeatures = feature.get('features');
              var extent = ol.extent.createEmpty();
              var j, jj;
              for (j = 0, jj = originalFeatures.length; j < jj; ++j) {
                ol.extent.extend(extent, originalFeatures[j].getGeometry().getExtent());
              }
              maxFeatureCount = Math.max(maxFeatureCount, jj);
              radius = 0.25 * (ol.extent.getWidth(extent) + ol.extent.getHeight(extent)) /
                  resolution;
              feature.set('radius', radius);
            }
          }
    
          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;
          }
    
          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;
          }
    
          vector = new ol.layer.Vector({
            source: new ol.source.Cluster({
              distance: 40,
              source: new ol.source.Vector({
                url: 'data/kml/2012_Earthquakes_Mag5.kml',
                format: new ol.format.KML({
                  extractStyles: false
                })
              })
            }),
            style: styleFunction
          });
    
          var raster = new ol.layer.Tile({
            source: new ol.source.Stamen({
              layer: 'toner'
            })
          });
    
          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
            })
          });
        </script>
      </body>
    </html>
     
    2015-11-21 00:28  693人阅读  评论(2)  收藏  举报
    0
      分类:
    OpenLayers(17) 
    0
    WebGIS(28) 
    0
    版权声明:本文为博主原创文章,未经博主允许不得转载。
    目录(?)[+]
          当某个地方发生一些事情之后,如果我们添加一个静态点在地图上,并不能引起注意,那我们可以放置一个动态的点,类似于在水中投入一个石头,水波扩散的效果,象征发生的事件有一定的影响区域,那么,我们如何利用 OpenLayers3 做出这样的效果呢?我们要实现的效果如下图,之前雅安发生过地震,我们在雅安放置一个这样的点,表示雅安发生了地震。
    0
    图1 点扩散效果图
          如何实现呢,我们首先想到,在矢量图层中添加一个点,对其赋予 ol.style.Icon 样式,然后将图片的 src属性赋值为一张 GIF 图片,那我们就进行尝试。

    注:文章中例子的完整代码,我放在了 GitHub 中,想看的朋友可以到 这个链接 看一下: https://github.com/QingyaFan/openlayers3-examples 。

    ol.style.Icon
    我们首先初始化一个矢量图层,并添加 Icon 样式,并添加到地图中:
    var gif_vector = new ol.layer.Vector({ source: new ol.source.Vector(), style: new ol.style.Style({ image: new ol.style.Icon({ anchor: [0.5, 45], anchorXUnits: 'fraction', anchorYUnits: 'pixel', opacity: 0.75, src: 'image/test_gif.gif' }) }) }); map.addLayer(gif_vector);
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    然后,我们给矢量图层添加点:
    gif_vector.getSource().addFeature(new ol.Feature({ geometry: new ol.geom.Point([0,0]), name: "testGif" }));
    • 1
    • 2
    • 3
    • 4
    实现效果并不是预期的情况:
    0
     
    图2 ol.style.Icon 效果
          本以为会出现动态的图标,结果发现,并没有,动态的图片变成了静态的图片。这里的原因,在这篇文章中就不具体展开了,以后会在深入研究中解释具体原因。那么,这条路走不通,还有没有其他的解决办法呢? 我们使用 CSS3 做出一个动画效果,并不是一件难事,但是动画效果需要依附于一个 HTML 元素,我们想到我们使用OpenLayers实现 Popup 效果时,Popup 就是一个 HTML 元素,其使用的技术就是 ol.Overlay,那么,我们试试ol.Overlay 这条路能否走通。
    ol.Overlay
    首先,创建一个 DIV 元素,将其形状限制为圆形,并使用 CSS3 为其赋予动画:
    HTML 元素
    <div id="css_animation">div>
    • 1
    CSS3 动画及其样式
    <style> #css_animation{ height:50px; 50px; border-radius: 25px; background: rgba(255, 0, 0, 0.9); transform: scale(0); animation: myfirst 3s; animation-iteration-count: infinite; } @keyframes myfirst{ to{ transform: scale(2); background: rgba(0, 0, 0, 0); } } style>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    这样,在页面中,其效果如下图:
    0
     
    图3 CSS3点扩散动画效果
    接下来,我们创建一个 overlay 实例,将这个 HTML 元素添加到 overlay 中:
    var point_div = document.getElementById("css_animation"); var point_overlay = new ol.Overlay({ element: point_div, positioning: 'center-center' }); map.addOverlay(point_overlay); point_overlay.setPosition([11468382.41282299,3502038.887913635]);
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
          我们来解释这段代码:首先,var point_div = document.getElementById("css_animation");获得具有动画效果的HTML元素;然后将其赋予 overlay 的element 参数,overlay 还有一个参数是 positioning: 'center-center',表示 HTML 元素相对于 overlay 的定位点的方位,”center-center” 表示元素中心对准定位点中心;最后 map.addOverlay(point_overlay); 将 overlay 添加到地图中,此时的 overlay 是不可见的,最后一行:point_overlay.setPosition([11468382.41282299,3502038.887913635]);设置了 overlay 可见元素(也就是具有动画的元素)的位置,这样动画元素就设置到相应的点了。
    这样,我们就实现了原来文章开头的效果。
    总结
          这篇文章中,我们为了实现动态点扩散效果,尝试了两种方法:一种是为点赋予 ol.style.Icon 样式,在相应的点覆盖一张动态 GIF 图片,然而并没有成功;另一种方法,是使用 ol.Overlay 结合 CSS3 动画,实现了相应的效果。
          需要注意的是 CSS3 动画是需要 IE10+ 、firefox(火狐)、chorme(谷歌)等现代浏览器的支持才可以实现,我们知道 OpenLayers3 利用了 HTML5 和 CSS3 的现代浏览器技术,也需要 IE9+支持才行。因此,要想得到好的效果,请使用现代浏览器。
    文章中例子的完整代码,我放在了 GitHub 中,想看的朋友可以到 这个链接 看一下: https://github.com/QingyaFan/openlayers3-examples 。
    好的,就写到这里,有什么问题,可以在文章下面留言。
  • 相关阅读:
    matlab 2021a 和 2021b共存的方案
    World Time Alighnment
    美化Xshell – 使用 Monokai 配色
    Centos 提示sudo: java: command not found解决办法
    typescript
    Spring MVC注册mapping
    Java执行JavaScript脚本
    vue3
    monaco editor
    rollup
  • 原文地址:https://www.cnblogs.com/devgis/p/16542712.html
Copyright © 2020-2023  润新知