vue+openlayers实现流动线效果(js源码导出方法直接复制使用即可生效)

在这里插入图片描述
注意:这里希望您已经引入了openlayers库
各个参数的含义

这里的参数只有第一个也就是lineFeature 是必传的其他的不传也行,有默认配置,效果达成后自己可以慢慢调试去定制效果

传入之前请不要给feature设置其他样式,用基本样式,对于定制样式我提供参数了,你也可以直接修改源代码

1.lineFeature 传入的线要素(传入之前请不要给其设置其他样式,用基本样式,对于定制样式我提供参数了,你也可以直接修改方法中的代码设置颜色)
2.FillColor 线段填充色(这里指的是整个线段的填充色 默认:#00A2E8)
3.lineDashColor 小线段的颜色 (流动线的颜色 默认:#FFFFFF)
4.fillWidth:填充线段的宽度 默认为2
5.lineDashWidth 小线段的宽度默认为2
6.lineDash 移动线段截取长度
7.lineDashOffsetSpeed 流动速度默认为2

源码:

import Style from "ol/style/Style.js";
import Stroke from 'ol/style/Stroke.js';
function markAnimateLine(lineFeature, FillColor = "#00A2E8", lineDashColor = "#FFFFFF", fillWidth = 2, lineDashWidth = 2, lineDash = [20, 27], lineDashOffsetSpeed=2,) {

    let style = lineStyleFunction()
    lineFeature.setStyle(style)
   function animateLine(){
       const lineStringStyle = lineFeature.getStyle();
       let lineDashOffset = lineStringStyle[1].getStroke().getLineDashOffset();
       lineDashOffset = lineDashOffset >= 2000 ? 0 : lineDashOffset + lineDashOffsetSpeed;

       // 修改线段要素的样式,将虚线偏移量更新到样式中
       lineStringStyle[1].getStroke().setLineDashOffset(lineDashOffset);
       lineFeature.setStyle(lineStringStyle);

       // 执行下一次动画
       requestAnimationFrame(animateLine);
    }
   function lineStyleFunction() {
       return [
           new Style({
            stroke: new Stroke({
                color: FillColor,  // 线的颜色
                width: fillWidth    // 线的宽度
            }),
        }),
        new Style({
            stroke: new Stroke({
                color: lineDashColor,
                width: lineDashWidth,
                lineDash: lineDash,
                lineDashOffset: 0
            })
        })
        ]
    }
    requestAnimationFrame(animateLine);
}
export default markAnimateLine

单独建一个js文件复制上面的代码
然后在vue文件中引入使用即可

import markAnimateLine from './animateLine'

在生成要素之后将该要素作为参数传入该方法(也可以传参改变样式,这里就是用默认配置了)

//示例
let lineString = new LineString(that.lineStringPointArr);
const lineStrinFeature = new Feature(lineString);
markAnimateLine(lineStrinFeature)

这里方法比较简陋,看代码思路很容易清晰,您可以修改代码让该方法的功能更加强大,如果解决了你的问题麻烦点个赞吧!谢谢嘿嘿嘿——

  • 10
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在 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实现图层控制控件了。需要注意的是,在实际应用中,可以根据需要自定义控件的样式和布局。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值