首页 > 其他分享 >Vue+Openlayer使用Draw实现交互式绘制多边形并获取面积

Vue+Openlayer使用Draw实现交互式绘制多边形并获取面积

时间:2023-03-24 15:34:43浏览次数:52  
标签:Draw Vue Openlayer area self output ol new import


场景

Vue+Openlayer使用Draw实现交互式绘制线段:

Vue+Openlayer使用Draw实现交互式绘制线段_BADAO_LIUMANG_QIZHI的博客-

在上面的基础上实现的交互式绘制线段,还可以实现绘制多边形并直接计算出面积。

Vue+Openlayer使用Draw实现交互式绘制多边形并获取面积_图层

注:

博客:BADAO_LIUMANG_QIZHI的博客_霸道流氓气质_

关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

1、导入基本模块

//导入基本模块
import "ol/ol.css";
import Map from "ol/Map";
import View from "ol/View";
import { Fill,Style,Stroke} from "ol/style";
//导入相关模块
import { Tile as TileLayer , Vector as VectorLayer } from 'ol/layer'
import { TileWMS ,Vector as VectorSource } from 'ol/source'
import { Select, Draw } from 'ol/interaction'
import { getArea } from "ol/sphere";

2、与前面博客相比,绘图工具传递的类型为Polygon

this.onAddInteraction('Polygon')

3、绘制结束时触发的事件中获取所绘制面积

//绘制结束时触发的事件
        this.draw.on('drawend', function(e) {
            const geometry = e.feature.getGeometry()
            var area = getArea(geometry);
            console.log("area="+area)
            var output;
              if (area > 10000) {
                output = (Math.round(area / 1000000 * 100) / 100) +
                  ' ' + 'km<sup>2</sup>';
              } else {
                output = (Math.round(area * 100) / 100) +
                  ' ' + 'm<sup>2</sup>';
              }
            console.log("output="+output)
            let pointArr = geometry.getCoordinates()
            self.coordinate.push(pointArr)
            console.log("self.coordinate="+self.coordinate);
            self.removeDraw()
        })

这里在绘制结束事的回调方法中直接获取geometry,然后调用ol自带的getArea方法,计算出面积

计算面积在线演示地址

ol-measure - CodeSandbox

Vue+Openlayer使用Draw实现交互式绘制多边形并获取面积_javascript_02

4、完整示例代码

<template>
    <div id="app">
      <div id="map" class="map"></div>
    </div>
</template>

<script>
//导入基本模块
import "ol/ol.css";
import Map from "ol/Map";
import View from "ol/View";
import { Fill,Style,Stroke} from "ol/style";
//导入相关模块
import { Tile as TileLayer , Vector as VectorLayer } from 'ol/layer'
import { TileWMS ,Vector as VectorSource } from 'ol/source'
import { Select, Draw } from 'ol/interaction'
import { getArea } from "ol/sphere";
export default {
  name: "olMapTileWMSDrawPolygon",
  data() {
    return {
      map: null, // map地图
      layer:null, //地图图层
      lineLayer:null, //线图层
      draw: null,
      lineSource:null,
      coordinate: [],
   };
  },
  mounted() {
    this.initMap();
  },
  methods: {

    // 绘图工具
    onAddInteraction(type) {
        let self = this
        //勾绘矢量图形的类
        this.draw = new Draw({
            //source代表勾绘的要素属于的数据集
            source: self.lineSource,
            //type 表示勾绘的要素包含的 geometry 类型
            type: type
        })

        //绘制结束时触发的事件
        this.draw.on('drawend', function(e) {
            const geometry = e.feature.getGeometry()
            var area = getArea(geometry);
            console.log("area="+area)
            var output;
              if (area > 10000) {
                output = (Math.round(area / 1000000 * 100) / 100) +
                  ' ' + 'km<sup>2</sup>';
              } else {
                output = (Math.round(area * 100) / 100) +
                  ' ' + 'm<sup>2</sup>';
              }
            console.log("output="+output)
            let pointArr = geometry.getCoordinates()
            self.coordinate.push(pointArr)
            console.log("self.coordinate="+self.coordinate);
            self.removeDraw()
        })
        self.map.addInteraction(this.draw)
    },
    //删除交互
    removeDraw() {
        this.map.removeInteraction(this.draw)
    },
    initMap() {
      //地图图层
      this.layer = new TileLayer({
        source: new TileWMS({
          //不能设置为0,否则地图不展示。
          ratio: 1,
          url: "http://localhost:8000/geoserver/nyc/wms",
          params: {
            LAYERS: "nyc:nyc_roads",
            STYLES: "",
            VERSION: "1.1.1",
            tiled: true
          },
          serverType: "geoserver",
        }),
      });

      //线的图层
      this.lineSource = new VectorSource({ wrapX: false });
      this.lineLayer = new VectorLayer({
          source: this.lineSource,
      });

      this.map = new Map({
        //地图容器ID
        target: "map",
        //引入地图
        layers: [this.layer,this.lineLayer],
        view: new View({
          //地图中心点
          center: [987777.93778, 213834.81024],
          zoom: 14,
          minZoom:6, // 地图缩放最小级别
          maxZoom:19,
        }),
      });

      // 获取点击地图的坐标(选中样式)
      let selectedStyle = new Style({
          fill: new Fill({
              color: 'rgba(1, 210, 241, 0.2)'
          }),
          stroke: new Stroke({
              color: 'yellow',
              width: 4
          })
      })
      // 选择线的工具类
      this.selectTool = new Select({
          multi: true,
          hitTolerance: 10, // 误差
          style: selectedStyle // 选中要素的样式
      })
      //添加交互
      this.map.addInteraction(this.selectTool)
      //调用绘图工具并传递类型为线,其他类型有Point,LineString,Polygon,Circle
      this.onAddInteraction('Polygon')

    },
  },
};
</script>

<style scoped>
.map {
  width: 100%;
  height: 800px;
}
</style>

标签:Draw,Vue,Openlayer,area,self,output,ol,new,import
From: https://blog.51cto.com/BADAOLIUMANGQZ/6147341

相关文章

  • Vue3+vite项目中如何动态导入并创建多个全局组件
    背景实际开发项目中,有些时候我们需要通过全局注册多个自定义组件,但是每个组件都导入一次,将会导致代码很冗余。实现方案customComponents/index.jsconstfiles=impor......
  • 【开发小记】vuex存储用户信息
    今天开发的过程中,遵循前人的经验对用户信息进行了存储。实现这个功能之后,我突然想到vuex本质是什么呢?它为什么要存在,以及它和浏览器的缓存的区别又是什么呢?(第三小节会给出......
  • Vue2响应式原理
    Vue.js基本上遵循MVVM(Model–View–ViewModel)架构模式,数据模型仅仅是普通的JavaScript对象。而当你修改它们时,视图会进行更新。本文讲解一下Vue响应式系统的底层细......
  • vue全家桶进阶之路2:JavaScript
    JavaScript(简称“JS”)是当前最流行、应用最广泛的客户端脚本语言,用来在网页中添加一些动态效果与交互功能,在Web开发领域有着举足轻重的地位。JavaScript与HTML和CSS......
  • vue全家桶进阶之路3:Node.js
    Node.js发布于2009年5月,由RyanDahl开发,是一个基于ChromeV8引擎的JavaScript运行环境,使用了一个事件驱动、非阻塞式I/O模型,让JavaScript运行在服务端的开发平台,它让......
  • vue-cli 初始化创建 vue2.9.6 项目路由守卫、封装axios、vuex
    阅读目录Vue如何封装Axios请求1安装axios2封装代码axios应用方式Vue中的路由守卫使用演示1全局守卫2组件级守卫3单个路由独享的守卫4官网整个路由守卫被触发流程的......
  • vue-cli 初始化创建 vue2.9.6 项目
    阅读目录vue-cli安装vue-cli初始化创建项目1、vueinit命令讲解2、初始化创建项目package.json项目结构1、build目录(webpack配置)2、config目录(vue项目配置目录)3、node_mod......
  • dockerfile部署vue+springboot+redis
    后端部署:一、拉取并启动redis镜像1、在服务器/usr/local/etc/redis/文件目录下建立redis.conf配置文件,配置信息如下:bind0.0.0.0protected-modeno2、拉取并启动最......
  • Vue.js 路由简介
    路由理解:一个路由(route)就是一组映射关系(key-value),多个路由需要路由器(router)进行管理。前端路由:key是路径,value是组件。......
  • Vuex 和 localStorage 的区别
    Vuex和localStorage的区别最重要的区别:vuex存储在内存中localstorage则以文件的方式存储在本地,只能存储字符串类型的数据,存储对象需要JSON的stringify和parse方......