vue+openlayers实现水波纹样式点(图层方式)

PS:以下所有的代码都是基于搭建好的环境后才能运行的

环境搭建,👉Vue+OpenLayers入门(加载OSM在线地图)

环境搭建好后,就可以开始愉快地写代码了。

step1:引入ol包里面需要用到的东西

import Feature from 'ol/Feature';
import {Point} from 'ol/geom';
import {Style, Fill, Stroke, Circle as CircleStyle} from 'ol/style';
import {Vector as VectorLayer} from 'ol/layer';
import VectorSource from 'ol/source/Vector';
import {fromLonLat} from 'ol/proj';

step2:新建图层

let pointFeature = new Feature({ // 新建一个点要素,此处地lonlat可以换成EPSG:3857下地任何坐标
  geometry: new Point(lonlat, 'XY'),
});
if (this.getLayerByName('dynamicLayer')) { // 判断地图上是否存在dynamicLayer这个图层,若存在,则清空图层上的要素后再添加要素
  this.getLayerByName('dynamicLayer').getSource().clear();
  this.getLayerByName('dynamicLayer').getSource().addFeature(pointFeature);
  return;
};
// 地图上不存在dynamicLayer时,新建dynamicLayer图层,并添加至地图
let dynamicPointLayer = new VectorLayer({
  source: new VectorSource({
    features: [pointFeature],
  }),
  name: 'dynamicLayer',
  zIndex: 2,
});
this.map.addLayer(dynamicPointLayer);

step3:绑定postcompose监听事件(重要)

let radius = 4; // radius一定要放在监听事件外面
let opacity = 1; // 波纹的透明度
this.map.on('postcompose', (event) => { // 给地图绑定事件,地图渲染后触发
  let dynamicFeatures = this.getLayerByName('dynamicLayer').getSource().getFeatures();
  opacity = (25 - radius) * (1 / 25) ; // 不透明度 radius为0时,不透明;radius为25时,透明
  dynamicFeatures.forEach((item) => {
    item.setStyle(new Style({
      image: new CircleStyle({
        radius: radius,
        stroke: new Stroke({
          color: 'rgba(255, 0, 0, '+ opacity +')', // 通过rgba控制波纹的产生和消失
          width: 2,
        }),
      })
    }));
  });
  radius = radius + 0.3;
  radius = radius >= 25 ? 4 : radius;
});
this.map.render(); // 触发地图绑定的postcompose事件

代码汇总:(addDynamicLayerToMap(lonlat)方法只需传入一个4326坐标系下的坐标便可使用)
例如:替换传入参数lonlat为 [104.70, 31.50],

 /**
  * 通过图层名称获取图层
  * @param {String}
  * @returns {Layer}
 */
 getLayerByName(name) {
   let allLayers = this.map.getLayers().getArray();
   let layer = undefined;
   if (allLayers.length) {
     for (let i = 0; i < allLayers.length; i++) {
       if (allLayers[i].get('name') === name) {
         layer = allLayers[i];
         break;
       }
     }
   }
   return layer;
 },
/** 
 * 添加水波纹点图层到地图 
 * @param lonlat EPSG:4326
 * */
addDynamicLayerToMap(lonlat) {
  lonlat = fromLonLat(lonlat); // 将4326的坐标转换为3857的坐标
  let pointFeature = new Feature({
    geometry: new Point(lonlat, 'XY'),
  });
  if (this.getLayerByName('dynamicLayer')) {
    this.getLayerByName('dynamicLayer').getSource().clear();
    this.getLayerByName('dynamicLayer').getSource().addFeature(pointFeature);
    return;
  };
  let dynamicPointLayer = new VectorLayer({
    source: new VectorSource({
      features: [pointFeature],
    }),
    name: 'dynamicLayer',
    zIndex: 2,
  });
  this.map.addLayer(dynamicPointLayer);
  let radius = 4; // radius一定要放在监听事件外面
  let opacity = 1;
  this.map.on('postcompose', (event) => {
    let dynamicFeatures = this.getLayerByName('dynamicLayer').getSource().getFeatures();
    opacity = (25 - radius) * (1 / 25) ; // 不透明度 radius为0时,不透明;radius为25时,透明
    dynamicFeatures.forEach((item) => {
      item.setStyle(new Style({
        image: new CircleStyle({
          radius: radius,
          stroke: new Stroke({
            color: 'rgba(255, 0, 0, '+ opacity +')',
            width: 2,
          }),
        })
      }));
    });
    radius = radius + 0.3;
    radius = radius >= 25 ? 4 : radius;
    this.map.render(); // 重要
  });
},

效果图:
在这里插入图片描述

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
要在 Vue 中使用 OpenLayers 实现图层控制控件,可以按照以下步骤进行操作: 1. 安装 OpenLayersVue: ``` npm install ol vue ``` 2. 在 Vue 中引入 OpenLayers: ```javascript import ol from 'ol' import 'ol/ol.css' ``` 3. 创建地图容器: ```html <template> <div ref="map" class="map"></div> </template> ``` 4. 在 Vue 的 mounted 钩子函数中创建地图: ```javascript mounted() { // 创建地图容器 const map = new ol.Map({ target: this.$refs.map, layers: [ new ol.layer.Tile({ source: new ol.source.OSM() }) ], view: new ol.View({ center: ol.proj.fromLonLat([116.3975, 39.9085]), zoom: 12 }) }); this.map = map; } ``` 5. 创建图层控制控件: ```html <template> <div ref="map" class="map"> <div class="ol-control ol-custom-control"> <div class="ol-custom-control-header">图层控制</div> <div class="ol-custom-control-body"> <div v-for="(layer, index) in layers" :key="index"> <input type="checkbox" :id="layer.name" v-model="layer.visible"> <label :for="layer.name">{{ layer.name }}</label> </div> </div> </div> </div> </template> ``` 6. 在 Vue 的 data 中定义图层数据和控件的状态: ```javascript data() { return { map: null, layers: [ { name: 'OSM', visible: true, layer: new ol.layer.Tile({ source: new ol.source.OSM() }) }, { name: 'Bing Maps', visible: false, layer: new ol.layer.Tile({ source: new ol.source.BingMaps({ key: 'your-bingmaps-api-key', imagerySet: 'Road' }) }) } ] } } ``` 7. 在 Vue 的 watch 中监听图层状态的变化并更新地图: ```javascript watch: { layers: { deep: true, handler(layers) { const map = this.map; map.getLayers().clear(); layers.forEach(layer => { if (layer.visible) { map.addLayer(layer.layer); } }); } } } ``` 通过以上步骤就可以在 Vue实现图层控制控件了。需要注意的是,在实际应用中,可以根据需要自定义控件的样式和布局。
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值