三十三、openlayers官网示例Drawing Features Style——在地图上绘制图形,并修改绘制过程中的颜色

ops/2024/9/25 16:51:04/

这篇讲的是使用Draw绘制图形时根据绘制形状设置不同颜色。

根据下拉框中的值在styles对象中取对应的颜色对象,new Draw的时候将其设置为style参数。

 const styles = {Point: {"circle-radius": 5,"circle-fill-color": "red",},LineString: {"circle-radius": 5,"circle-fill-color": "red","stroke-color": "yellow","stroke-width": 2,},Polygon: {"circle-radius": 5,"circle-fill-color": "red","stroke-color": "yellow","stroke-width": 2,"fill-color": "blue",},Circle: {"circle-radius": 5,"circle-fill-color": "red","stroke-color": "blue","stroke-width": 2,"fill-color": "yellow",},};const typeSelect = document.getElementById("type");let draw; function addInteraction() {const value = typeSelect.value;if (value !== "None") {draw = new Draw({source: source,type: typeSelect.value,style: styles[value],});map.addInteraction(draw);}}

 需要注意的是这个style是绘制过程中的颜色,如果需要设置绘制完成后的颜色还得在图层中设置

  const source = new VectorSource({ wrapX: false });const vector = new VectorLayer({source: source,style:style});

 完整代码:

<template><div class="box"><h1>Drawing Features Style绘制不同颜色的图形</h1><div id="map"></div><div class="row"><div class="col-auto"><span class="input-group"><label class="input-group-text" for="type">Geometry type:</label><select class="form-select" id="type"><option value="Point">Point</option><option value="LineString">LineString</option><option value="Polygon">Polygon</option><option value="Circle">Circle</option><option value="None">None</option></select></span></div></div></div>
</template><script>
import Draw from "ol/interaction/Draw.js";
import Map from "ol/Map.js";
import View from "ol/View.js";
import { OSM, Vector as VectorSource } from "ol/source.js";
import { Tile as TileLayer, Vector as VectorLayer } from "ol/layer.js";
export default {name: "",components: {},data() {return {map: null,};},computed: {},created() {},mounted() {const raster = new TileLayer({source: new OSM(),});const source = new VectorSource({ wrapX: false });const vector = new VectorLayer({source: source,});const map = new Map({layers: [raster, vector],target: "map",view: new View({center: [-11000000, 4600000],zoom: 4,}),});const styles = {Point: {"circle-radius": 5,"circle-fill-color": "red",},LineString: {"circle-radius": 5,"circle-fill-color": "red","stroke-color": "yellow","stroke-width": 2,},Polygon: {"circle-radius": 5,"circle-fill-color": "red","stroke-color": "yellow","stroke-width": 2,"fill-color": "blue",},Circle: {"circle-radius": 5,"circle-fill-color": "red","stroke-color": "blue","stroke-width": 2,"fill-color": "yellow",},};const typeSelect = document.getElementById("type");let draw; function addInteraction() {const value = typeSelect.value;if (value !== "None") {draw = new Draw({source: source,type: typeSelect.value,style: styles[value],});map.addInteraction(draw);}}typeSelect.onchange = function () {map.removeInteraction(draw);addInteraction();};addInteraction();},methods: {},
};
</script><style lang="scss" scoped>
#map {width: 100%;height: 500px;
}
.box {height: 100%;
}#info {width: 100%;height: 24rem;overflow: scroll;display: flex;align-items: baseline;border: 1px solid black;justify-content: flex-start;
}
</style>


http://www.ppmy.cn/ops/44936.html

相关文章

kindeditor 上传中 网络图片 去掉 图片空间

kindeditor 上传中 “网络图片”功能里有个 “图片空间”可以浏览全站目录&#xff0c;这个功能对管理员来讲尚可&#xff0c;如是前台提供给普通用户使用就不妙了&#xff0c;因此需要关闭这个功能。 可以在 JS控制里面如类似下图 &#xff1a; K.create(#forumSendFormEdit…

数据大屏vue3+ts+axios+MockJS+dataV+echarts

一、官网/文档 vue3&#xff1a;https://cn.vuejs.org/api/TypeScript&#xff1a;https://www.tslang.cn/docs/handbook/basic-types.htmlaxios&#xff1a;http://www.axios-js.com/zh-cn/docs/MockJS&#xff1a;http://mockjs.com/dataV&#xff1a;http://datav.jiamingh…

有源蜂鸣器、无源蜂鸣器区别

对比 有源蜂鸣器 1. 结构和原理 有源蜂鸣器内部自带振荡源&#xff0c;只需接通电源即可发声。内部电路会自动产生一定频率的振荡信号&#xff0c;从而驱动蜂鸣器发声。 2. 驱动方式 驱动有源蜂鸣器非常简单&#xff0c;只需要提供一个直流电源&#xff08;通常是3V、5V或…

vue中v-for的key值怎么使用?如何选择?

在 Vue 中&#xff0c;v-for 指令用于渲染列表数据。当使用 v-for 时&#xff0c;强烈建议为每一项提供一个唯一的 key 属性。这个 key 不仅是 Vue 区分节点的标识&#xff0c;也是 Vue 实现列表高效更新的一种机制。 如何使用 key 在 v-for 中&#xff0c;key 应该绑定到列表…

【408真题】2009-28

“接”是针对题目进行必要的分析&#xff0c;比较简略&#xff1b; “化”是对题目中所涉及到的知识点进行详细解释&#xff1b; “发”是对此题型的解题套路总结&#xff0c;并结合历年真题或者典型例题进行运用。 涉及到的知识全部来源于王道各科教材&#xff08;2025版&…

一篇文章搞懂二叉树

文章目录 DP 树叶的度树的度节点的层次节点的祖先节点的子孙双亲节点或父节点 树的表示孩子兄弟表示法双亲表示法树和非树树的应用 二叉树满二叉树完全二叉树推论二叉树的存储以数组的方式以链表的方式堆(Heap)堆的分类大根堆和小根堆的作用 二叉树的遍历DFS和BFS DP 动态规划…

SPI协议的基本介绍

1. 基本介绍 SPI&#xff08;Serial Peripheral Interface&#xff0c;串行外设接口&#xff09;是一种高速、全双工、同步的通信协议&#xff0c;主要用于微控制器和各种外部硬件或外设之间的通信&#xff0c;例如传感器、SD卡、液晶显示屏等。 SPI协议由四根线组成&#xff1…

Selenium 自动化测试工具(1) (Selenium 工作原理,常用API的使用)

文章目录 什么是自动化测试什么是测试工具&#xff1a;Selenium 工作原理(重要)Selenium API定位元素CSS 选择器xpath 定位元素 通过Java代码实现自动化1. 定位元素2. 关闭浏览器3. 获取元素文本4. 鼠标点击与键盘输入5. 清空内容6.打印信息 什么是自动化测试 关于自动化&…