openlayers 绘图功能,绘制多边形,draw组件的使用,一个简单的需求引发的思考(二)

news2024/9/23 1:19:23

上一篇是使用openlayers原生实现的,这一节使用vue3-openlayers实现(有轮子真好)

1 需求

使用openlayers绘图功能绘制多边形

2 分析

主要是openlayers中draw功能的使用

3 实现

为了方便,就不加载底图了,直接使用绘制功能

2.1 简单实现

<template>
  <ol-map
    :loadTilesWhileAnimating="true"
    :loadTilesWhileInteracting="true"
    style="width: 100%; height: 100%"
  >
    <ol-view
      ref="view"
      :center="center"
      :rotation="rotation"
      :zoom="zoom"
      :projection="projection"
    />
		<ol-vector-layer>
      <ol-source-vector :projection="projection">
        <ol-interaction-draw
          :type="'Polygon'"
	       :features="features"
	       :source="source"
          @drawend="drawend"
          @drawstart="drawstart"
        >
          <ol-style>
            <ol-style-stroke color="rgba(228, 147, 87, 1)" :width="2"></ol-style-stroke>
            <ol-style-fill color="rgba(228, 147, 87, 0.5)"></ol-style-fill>
            <!-- <ol-style-circle :radius="5">
              <ol-style-fill color="#00dd11" />
              <ol-style-stroke color="blue" :width="2" />
            </ol-style-circle> -->
          </ol-style>
        </ol-interaction-draw>
      </ol-source-vector>

      <ol-style>
        <ol-style-stroke color="red" :width="2"></ol-style-stroke>
        <ol-style-fill color="rgba(255,0,0,0.3)"></ol-style-fill>
        <ol-style-circle :radius="7">
          <ol-style-fill color="red"></ol-style-fill>
        </ol-style-circle>
      </ol-style>
    </ol-vector-layer>
  </ol-map>
</template>

<script setup lang="ts">
import { DrawEvent } from 'ol/interaction/Draw';
const center = ref([121, 31]);
const projection = ref('EPSG:4326');
const zoom = ref(5);
const rotation = ref(0);
const features=ref([])
const source=ref([])


const drawstart = (event:Event) => {
  console.log(event);
};

const drawend = (event:DrawEvent) => {
  console.log(event.feature.getGeometry());
};
</script>
<style scoped lang="scss">

</style>


2.2 进阶实现(overrideStyleFunction属性的使用)

<template>
  <ol-map
    :loadTilesWhileAnimating="true"
    :loadTilesWhileInteracting="true"
    style="width: 100%; height: 100%"
  >
    <ol-view
      ref="view"
      :center="center"
      :rotation="rotation"
      :zoom="zoom"
      :projection="projection"
    />
    <ol-vector-layer>
      <ol-source-vector :projection="projection">
        <ol-interaction-draw
          :type="'Polygon'"
          :features="features"
          :source="source"
          @drawend="drawend"
          @drawstart="drawstart"
        >
          <ol-style :overrideStyleFunction="handleStyleFunction">
            <!-- <ol-style-stroke color="rgba(228, 147, 87, 1)" :width="2"></ol-style-stroke>
            <ol-style-fill color="rgba(228, 147, 87, 0.5)"></ol-style-fill> -->
            <!-- <ol-style-circle :radius="5">
              <ol-style-fill color="#00dd11" />
              <ol-style-stroke color="blue" :width="2" />
            </ol-style-circle> -->
          </ol-style>
        </ol-interaction-draw>
      </ol-source-vector>

      <ol-style :overrideStyleFunction="styleFunction">
        <!-- <ol-style-stroke color="red" :width="2"></ol-style-stroke>
        <ol-style-fill color="rgba(255,0,0,0.3)"></ol-style-fill>
        <ol-style-circle :radius="7">
          <ol-style-fill color="red"></ol-style-fill>
        </ol-style-circle> -->
      </ol-style>
    </ol-vector-layer>
  </ol-map>
</template>

<script setup lang="ts">
import { FeatureLike } from 'ol/Feature';
import { LineString, Point } from 'ol/geom';
import { DrawEvent } from 'ol/interaction/Draw';
import { Circle, Fill, Stroke, Style } from 'ol/style';
const center = ref([121, 31]);
const projection = ref('EPSG:4326');
const zoom = ref(5);
const rotation = ref(0);
const features = ref([]);
const source = ref([]);

const drawstart = (event: Event) => {
  console.log(event);
};

const drawend = (event: DrawEvent) => {
  console.log(event.feature.getGeometry());
};

const handleStyleFunction = (feature: FeatureLike, currentStyle: Style) => {
  const geometry = feature.getGeometry();
  const coord = geometry.getCoordinates();
  const type = geometry.getType();
  const styles: Array<Style> = [];
  for (let i = 0; i < coord.length - 1; i++) {
    styles.push(
      new Style({
        geometry: new Point(coord[i]),
        image: new Circle({
          fill: new Fill({ color: [255, 255, 255, 1] }),
          stroke: new Stroke({
            color: 'rgba(228, 147, 87, 1)',
            width: 2
          }),
          radius: 4
        })
      })
    );
  }
  if (type === 'LineString') {
    for (let i = 0; i < coord.length - 1; i++) {
      styles.push(
        new Style({
          geometry: new LineString([coord[i], coord[i + 1]]),
          stroke: new Stroke({
            color: 'orange',
            lineDash: coord.length > 2 && i < coord.length - 2 ? [] : [10],
            width: 2
          })
        })
      );
    }
  }
  return styles;
};

const styleFunction = (feature: FeatureLike, currentStyle: Style) => {
  const styles = [];
  const coord = feature.getGeometry().getCoordinates();

  for (let i = 0; i < coord[0].length - 1; i++) {
    styles.push(
      new Style({
        geometry: new Point(coord[0][i]),
        image: new Circle({
          radius: 4,
          fill: new Fill({
            color: '#ffff'
          }),
          stroke: new Stroke({
            color: 'orange',
            width: 2
          })
        })
      })
    );
  }
  styles.push(
    new Style({
      fill: new Fill({
        color: [128, 128, 255, 0.5]
      }),
      stroke: new Stroke({
        color: 'blue',
        width: 2
      })
    })
  );
  return styles;
};
</script>
<style scoped lang="scss"></style>


存在问题:与上一篇一样,在点击鼠标后并且鼠标有移动时,前一段虚线才会变成实线,并且顶点加上Point,因为只有鼠标移动才会有新的坐标点加入到绘制,因为openlayers没有提供绘制过程中的钩子函数,所以只能是当前效果

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1813851.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

ttkbootstrap的icon图标自定义

前言 在使用ttkbootstrap库时&#xff0c;发现icon参数使用报错&#xff0c;错误代码 root ttk.Window(themename"superhero",size(1400, 700),resizable(True, True),iconphoto"1.png" )结果报错&#xff1a;iconphoto path is bad; using default ima…

AC/DC电源模块的原理、特点以及其在实际应用中的重要性

BOSHIDA AC/DC电源模块的原理、特点以及其在实际应用中的重要性 AC/DC电源模块是一种用于将交流电转换为直流电的设备&#xff0c;广泛应用于各种电子设备中。这种电源模块可以有效地将电力从电网中提取出来&#xff0c;并将其转换为稳定的直流电源&#xff0c;供给各种不同功…

【短剧看剧系统之投流版】短剧看剧系统功能更新,前端uniapp搭建开发

目录 一、常规款短剧系统和投流版的区别&#xff1f; 二、后端体系 1.管理端&#xff1a; 2.代理投流端 三、功能区别 总结&#xff1a; 前言&#xff1a; 短剧看剧系统目前在抖音端是比较热门的&#xff0c;最重要的功能就是可以接入第三方cps&#xff0c;包含类目报白…

万众瞩目的苹果AI来了,但我们用不了

关注卢松松&#xff0c;会经常给你分享一些我的经验和观点。 从今天开始&#xff0c;最了解你的不是你老婆&#xff0c;不是你自己&#xff0c;而是苹果AI。 万众瞩目的苹果WWDC24开发者大会在大半夜举办了&#xff0c;其中一项重要的更新是&#xff1a;苹果宣布要把ChatGPT集…

MES系统定制 | 生产调度车间排班计划/MES排程排产

MES系统是一种集成化的生产信息化管理系统&#xff0c;通过实时收集和分析车间生产数据&#xff0c;帮助企业实现生产过程的自动化控制和监测。它可以跟踪生产计划、设备状态、物料流动等关键指标&#xff0c;并提供实时报表和决策支持。在这个系统中&#xff0c;车间班次排班是…

零基础直接上手java跨平台桌面程序,使用javafx(五)TableView显示excel表

我们在窗口的中间加上TableVie&#xff1a; 在hello-view.fxml的文本中&#xff0c;要增加一些代码。在TableView定义中加上fx:id"TableView1"&#xff0c;这样java代码才方便访问&#xff0c;在java代码中要加上FXML private TableView TableView1;表示定义TableVie…

如何禁止使用U盘|禁止使用U盘的四个方法

你知道U盘滥用对企业的危害&#xff0c;总接下来有这三点: 数据泄露&#xff1a;U盘可以方便地存储和传输大量数据&#xff0c;但如果U盘丢失或被盗&#xff0c;其中的数据可能会被他人获取&#xff0c;从而导致数据泄露。病毒传播&#xff1a;U盘是病毒传播的常见途径之一。如…

基于若依的ruoyi-nbcio-plus里抄送人多页选择人员的bug修复

更多ruoyi-nbcio功能请看演示系统 gitee源代码地址 前后端代码&#xff1a; https://gitee.com/nbacheng/ruoyi-nbcio 演示地址&#xff1a;RuoYi-Nbcio后台管理系统 http://218.75.87.38:9666/ 更多nbcio-boot功能请看演示系统 gitee源代码地址 后端代码&#xff1a; h…

点云技术在AI绘画中的革新性应用

引言&#xff1a; 随着人工智能的不断演进&#xff0c;艺术与科技的交融催生了AI绘画这一全新的创作方式。AI绘画不仅为艺术家提供了前所未有的工具&#xff0c;也拓展了艺术表达的边界。在这一进程中&#xff0c;点云技术作为一种重要的三维数据处理手段&#xff0c;其在AI绘画…

深入解析:常用的IP地址类型及其应用

随着互联网的日益发展&#xff0c;IP地址已经成为了我们日常生活中不可或缺的一部分。无论是浏览网页、发送邮件&#xff0c;还是进行在线视频通话&#xff0c;都离不开IP地址的参与。然而&#xff0c;对于许多非专业人士来说&#xff0c;IP地址的分类及其应用可能还是一个相对…

1502 - JUC高并发

慢慢挣&#xff0c;今天比昨天更有钱&#xff0c;明天比今天有钱&#xff0c;后天比明天有钱。 0.思维导图 6.多线程锁 synchronized实现同步的基础&#xff1a;Java中的每一个对象都可以作为锁。 具体表现为以下3中形式 对于普通同步方法&#xff0c;锁是当前实例对象。对于…

Python 基础001 pythonpycharm安装

1 安装python 尽量在官网安装 根据电脑情况下载,下载完需要重启电脑 python安装路径自定义 添加环境变量&#xff08;add path&#xff09;需要勾选&#xff0c;若无勾选&#xff0c;手动更新环境变量 确认python是否安装成功&#xff1a; 方法一&#xff1a;有安装成功&am…

零基础直接上手java跨平台桌面程序,使用javafx(六)查询sqlite数据显示到TableView中

我们使用jdbc查询sqlite的一个表显示到TableView中 在hello-view的onMouseClicked里面填上“openclick2”&#xff0c;然后在HelloController写上openclick2的相关代码FXML protected void openclick2() { }。我们要先配置好sqlite的jdbc驱动&#xff08;略&#xff09;。openc…

代码随想录算法训练营第二十九天【回溯】| 491,46,47

491. Non-decreasing Subsequences 排列用startindex 树枝不去重&#xff0c;树层去重 子集问题结果在结点&#xff08;个数>2&#xff09; class Solution(object):def findSubsequences(self, nums):""":type nums: List[int]:rtype: List[List[int]]&…

如何在网上下载到最新或者历史QGIS各个版本的C++源码

背景&#xff1a; 博主写下这篇文章的时候已经是PyQGIS下二开了两年&#xff0c;开发一些功能必须得去阅读QGIS的C版本源码&#xff0c;还得考虑到QGIS的长期稳定版和最新版的源码区别。 所以如何去下载到QGIS的源码&#xff0c;就成了当务之急。 QGIS3.36.3的C源码长得像这…

KOL营销在时尚、美妆与健康行业的特点解析与应用策略

在当今数字化时代&#xff0c;KOL营销已经成为推动品牌影响力和销售增长的重要策略之一。尤其在时尚、美妆和健康等行业&#xff0c;KOL的影响力和效果尤为显著。本文Nox聚星将和大家详细探讨KOL营销在这些行业中的应用情况、特点以及最佳实践。 一、时尚行业KOL营销 时尚行业…

finalshell创建和使用

1、下载安装 下载地址Finalshell SSH工具,业界最强大的SSH客户机 2、创建连接 点击文件夹新建连接

Struts2 系列漏洞 - S2-003、S2-005

一、前言 前面一篇文章也有提到 struts2 在进入 action 进行逻辑处理前&#xff08;以及逻辑处理后&#xff09;&#xff0c;会进入 18 个拦截器栈中对请求进行必要的处理&#xff08;如果没有自定义拦截器的话&#xff0c;可以在 struts-default.xml 中找到相应的拦截器栈&am…

【学习笔记】使用gcc编译程序并检查依赖的库

编译 gcc echo.c -o app -lfcgi-o app&#xff1a;指定编译后的输出文件名为 app。 -lfcgi&#xff1a;告诉编译器链接 FastCGI 库。 检查 ldd appldd 是一个在 Unix 和类 Unix 系统中用来打印一个已编译的程序所依赖的共享库列表的命令。当你运行 ldd app 命令时&#xff0…

Windows 10 中添加 开机启动 自动运行的程序

Win R 输入 “shell:startup” &#xff0c;回车确定 把 应用的 快捷方式 复制到文件夹中。就会自动开机启动了 参照图&#xff1a;