Vue+penlayers实现多边形绘制及展示


Posted in Vue.js onDecember 24, 2020

本文实例为大家分享了Vue+penlayers实现多边形绘制展示代码,供大家参考,具体内容如下

<!--
 * @Description: 绘制多边形
 * @Author: Dragon
 * @Date: 2020-12-17 16:02:06
 * @LastEditTime: 2020-12-18 17:20:33
 * @LastEditors: Dragon
-->

<template>
 <div>
  <div class="query-wrap">
   <el-button type="primary" @click="drawStart('Polygon')">
    {{ isDraw ? "绘制区域" : "重新绘制" }}
   </el-button>
  </div>
  <div id="map"></div>
 </div>
</template>

<script>
import "ol/ol.css";
import { Map, View } from "ol";
import { Image as ImageLayer, Vector as VectorLayer } from "ol/layer";
import { ImageStatic, Vector as VectorSource } from "ol/source";
import { getCenter } from "ol/extent";
import { Projection } from "ol/proj";

import Draw from "ol/interaction/Draw";
import { Style, Fill, Stroke } from "ol/style";

import { GeoJSON } from "ol/format";
import staticMap from "@/assets/map.png";

export default {
 data() {
  return {
   map: null, // 地图
   imgx: 0, // 当前地图宽
   imgy: 0, // 当前地图高
   isDraw: true, // 是否绘制
   draw: null,
   source: null,
   vector: null,
   styles: [
    new Style({
     stroke: new Stroke({
      color: "rgba(255,0,0,0.6)",
      width: 2,
     }),
     fill: new Fill({
      color: "rgba(255,0,0,0.3)",
     }),
    }),
   ],
   geojsonObject: {
     'type': 'FeatureCollection',
     'features': [
      {
       'type': 'Feature',
       'geometry': {
        'type': 'Polygon',
        'coordinates': [
         [
          [97.16862961519749, 322.26517247174047],
          [117.3211820327625, 481.9353954724479],
          [1.056456546810466, 489.6863771715114],
          [13.458027265312012, 320.71497613192776],
          [97.16862961519749, 322.26517247174047]
                   ]
        ],
       },
      },
     ],
    },
  };
 },
 methods: {
  // 初始化地图
  initMap() {
   let extent = [0, 0, this.imgx, this.imgy];
   let projection = new Projection({
    code: "xkcd-image",
    units: "pixels",
    extent: extent,
   });
   let $this = this;
   this.map = new Map({
    target: "map",
    layers: [
     new ImageLayer({ // 展示地图层
      source: new ImageStatic({
       url: staticMap,
       projection: projection,
       imageExtent: extent,
      }),
     }),
     new VectorLayer({
      source: new VectorSource({
       features: new GeoJSON().readFeatures($this.geojsonObject),
      }),
      style: $this.styles,
     }),
    ],
    view: new View({
     projection: projection,
     center: getCenter(extent),
     zoom: 2,
     maxZoom: 18,
    }),
   });

   this.source = new VectorSource({ wrapX: false })
   this.vector = new VectorLayer({
    source: this.source,
    style: this.styles
   })
   this.map.addLayer(this.vector)
  },
  
  // 开始绘制多边形
  drawStart(type) {
   let that = this;
   if(this.isDraw) {
    this.isDraw = false
    this.draw = new Draw({
     source: this.source,
     type: type,
    });
    this.map.addInteraction(this.draw);
    this.draw.on("drawend", function (evt) {
     that.drawingEnd(evt);
    });
   } else {
    this.source.clear()
    this.map.removeInteraction(this.draw);
    this.isDraw = true
   }
   
  },

  // 构建多边形结束
  drawingEnd(evt) {
   let that = this
   const geo = evt.feature.getGeometry();
   const t = geo.getType();
   if (t === "Polygon") {
    // 获取坐标点
    const points = geo.getCoordinates();
    console.warn(points, "绘制结束,点坐标")
    this.map.removeInteraction(this.draw); // 移除绘制
   }
  },
 },
 mounted() {
  let that = this;
  let img = new Image();
  setTimeout(function() {
   img.src = staticMap;
   img.onload = function (res) {
    that.imgx = res.target.width;
    that.imgy = res.target.height;
    that.initMap();
   };
  }, 500)
  
 },
};
</script>

<style>
#map {
 width: 100%;
 height: calc(100vh - 50px);
}
</style>

效果图:

Vue+penlayers实现多边形绘制及展示

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Vue.js 相关文章推荐
vue3.0+vue-router+element-plus初实践
Dec 02 Vue.js
vue使用echarts画组织结构图
Feb 06 Vue.js
vue-cli中实现响应式布局的方法
Mar 02 Vue.js
vue路由实现登录拦截
Mar 24 Vue.js
vue backtop组件的实现完整代码
Apr 07 Vue.js
浅谈vue2的$refs在vue3组合式API中的替代方法
Apr 18 Vue.js
解读Vue组件注册方式
May 15 Vue.js
详解vue中v-for的key唯一性
May 15 Vue.js
Vue Element UI自定义描述列表组件
May 18 Vue.js
vue3引入highlight.js进行代码高亮的方法实例
Apr 08 Vue.js
使用vuex-persistedstate本地存储vuex
Apr 29 Vue.js
vue实现input输入模糊查询的三种方式
Aug 14 Vue.js
Vue使用鼠标在Canvas上绘制矩形
Dec 24 #Vue.js
vue绑定class的三种方法
Dec 24 #Vue.js
全面解析Vue中的$nextTick
Dec 24 #Vue.js
vue实现登录、注册、退出、跳转等功能
Dec 23 #Vue.js
vue下拉刷新组件的开发及slot的使用详解
Dec 23 #Vue.js
Vue3 实现双盒子定位Overlay的示例
Dec 22 #Vue.js
详解Vue的异步更新实现原理
Dec 22 #Vue.js
You might like
php利用iframe实现无刷新文件上传功能的代码
2011/09/29 PHP
PHP中header用法小结
2016/05/23 PHP
php接口技术实例详解
2016/12/07 PHP
PHP常用字符串输出方法分析(echo,print,printf及sprintf)
2021/03/09 PHP
this和执行上下文实现代码
2010/07/01 Javascript
面向对象设计模式的核心法则
2013/11/10 Javascript
jQuery简单实现禁用右键菜单
2015/03/10 Javascript
jQuery满意度星级评价插件特效代码分享
2015/08/19 Javascript
JavaScript给input的value赋值引发的关于基本类型值和引用类型值问题
2015/12/07 Javascript
javascript 中的事件委托详解
2016/10/25 Javascript
js正则表达式验证密码强度【推荐】
2017/03/03 Javascript
javascript图片预览和上传(兼容IE)
2017/03/15 Javascript
Vue核心概念Getter的使用方法
2019/01/18 Javascript
layui 实现自动选择radio单选框(checked)的方法
2019/09/03 Javascript
微信小程序实现左滑删除效果
2020/11/18 Javascript
JavaScript实现点击切换功能
2021/01/27 Javascript
python 生成目录树及显示文件大小的代码
2009/07/23 Python
python中pycurl库的用法实例
2014/09/30 Python
Scrapy爬虫实例讲解_校花网
2017/10/23 Python
Python实现pdf文档转txt的方法示例
2018/01/19 Python
解决在pycharm中显示额外的 figure 窗口问题
2019/01/15 Python
python 使用pdfminer3k 读取PDF文档的例子
2019/08/27 Python
关于Flask项目无法使用公网IP访问的解决方式
2019/11/19 Python
Python使用pdb调试代码的技巧
2020/05/03 Python
HTML5+CSS3实现无插件拖拽上传图片(支持预览与批量)
2017/01/05 HTML / CSS
HTML5移动端开发遇见的东西
2019/10/11 HTML / CSS
HTML5 Canvas 旋转风车绘制
2017/08/18 HTML / CSS
馥绿德雅美国官方网站:Rene Furterer头皮护理专家
2019/05/01 全球购物
梅西百货官网:Macy’s
2020/08/04 全球购物
解释一下Windows的消息机制
2014/01/30 面试题
关于迟到的检讨书
2014/01/26 职场文书
国际商贸专业自荐信
2014/06/09 职场文书
该怎么书写道歉信?
2019/07/03 职场文书
OpenCV-Python实现人脸美白算法的实例
2021/06/11 Python
如何用python清洗文件中的数据
2021/06/18 Python
微软发布Windows 11今年最大更新22H2(附 ISO 镜像官方下载)
2022/09/23 数码科技