当前位置: 首页 > news >正文

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

这篇讲的是使用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.lryc.cn/news/357496.html

相关文章:

  • Vue——事件修饰符
  • Go语言GoFly框架快速新增接口/上手写代码
  • 【Vue】v-else 和 v-else-if
  • 一致性hash算法原理图和负载均衡原理-urlhash与least_conn案例
  • MySQL建库
  • 系统资源监控器工具glances的使用详解
  • JDBC使用QreryRunner简化SQL查询注意事项
  • 前缀和(下)
  • 【排序算法】希尔排序
  • 数学建模--LaTex插入表格详细介绍
  • 未来已来:Flutter引领的安卓与跨平台开发奇幻之旅
  • 如何将Windows PC变成Wi-Fi热点?这里提供详细步骤
  • 报错:Cannot invoke “springfox.documentation.service.ParameterType.getIn()“
  • 一个生动的例子——通过ERC20接口访问Tether合约
  • 新媒体时代,LCD电子价签赋予零售场景新活力
  • 芋道源码 / yudao-cloud:前端技术架构探索与实践
  • 2024 angstromCTF re 部分wp
  • STL库--priority_queue
  • 网络编程 —— Http使用httpClient实现页面爬虫
  • 【本地运行chatgpt-web】启动前端项目和service服务端项目,也是使用nodejs进行开发的。两个都运行成功才可以使用!
  • TOGAF企业架构章节(核心)知识点(一)
  • 手摸手教你uniapp原生插件开发
  • C++进程间通信 消息队列
  • mysql中InnoDB的统计数据
  • P459 包装类Wrapper
  • Kong网关的负载均衡
  • 这是一个逗号
  • oracle tree
  • react-beautiful-dnd 横纵排序demo
  • web练习