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 相关文章推荐
Vue 的 v-model用法实例
Nov 23 Vue.js
vue实现购物车的小练习
Dec 21 Vue.js
Vue 3自定义指令开发的相关总结
Jan 29 Vue.js
Vue如何实现变量表达式选择器
Feb 18 Vue.js
基于vue-simple-uploader封装文件分片上传、秒传及断点续传的全局上传插件功能
Feb 23 Vue.js
Vue基本指令实例图文讲解
Feb 25 Vue.js
浅谈vue2的$refs在vue3组合式API中的替代方法
Apr 18 Vue.js
vue实现同时设置多个倒计时
May 20 Vue.js
Vue实现tab导航栏并支持左右滑动功能
Jun 28 Vue.js
Vue提供的三种调试方式你知道吗
Jan 18 Vue.js
vue配置型表格基于el-table拓展之table-plus组件
Apr 12 Vue.js
vue3种table表格选项个数的控制方法
Apr 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实现百度网盘图片直链的代码分享
2012/11/01 PHP
php过滤HTML标签、属性等正则表达式汇总
2014/09/22 PHP
浅谈Coreseek、Sphinx-for-chinaese、Sphinx+Scws的区别
2016/12/15 PHP
PHP实现函数内修改外部变量值的方法示例
2018/12/28 PHP
XHTML-Strict 内允许出现的标签
2006/12/11 Javascript
在firefox和Chrome下关闭浏览器窗口无效的解决方法
2014/01/16 Javascript
Jquery在指定DIV加载HTML示例代码
2014/02/17 Javascript
jQuery选择器简明总结(含用法实例,一目了然)
2014/04/25 Javascript
JavaScript判断FileUpload控件上传文件类型
2015/09/28 Javascript
JavaScript的设计模式经典之建造者模式
2016/02/24 Javascript
深入浅析JavaScript中的Function类型
2016/07/09 Javascript
javascript汉字拼音互转的简单实例
2016/10/09 Javascript
jQuery+HTML5实现弹出创意搜索框层
2016/12/29 Javascript
js指定步长实现单方向匀速运动
2017/07/17 Javascript
vue教程之toast弹框全局调用示例详解
2020/08/24 Javascript
基于vue.js中关于下拉框的值默认及绑定问题
2018/08/22 Javascript
python基于phantomjs实现导入图片
2016/05/13 Python
python的pip安装以及使用教程
2018/09/18 Python
对django layer弹窗组件的使用详解
2019/08/31 Python
Python Django view 两种return的实现方式
2020/03/16 Python
PyPDF2读取PDF文件内容保存到本地TXT实例
2020/05/12 Python
浅谈pycharm导入pandas包遇到的问题及解决
2020/06/01 Python
浅谈Selenium+Webdriver 常用的元素定位方式
2021/01/13 Python
大女孩胸罩:Big Girls Bras
2016/12/15 全球购物
美国孩之宝玩具官网:Hasbro Pulse
2019/06/24 全球购物
购买正版游戏和游戏激活码:Green Man Gaming
2019/11/06 全球购物
澳大利亚窗帘商店:Curtain Wonderland
2019/12/01 全球购物
保险专业自荐信范文
2014/02/20 职场文书
计算机应用应届生求职信
2014/07/12 职场文书
个人批评与自我批评
2014/10/15 职场文书
2014年收银工作总结
2014/11/13 职场文书
2014年基建工作总结
2014/12/12 职场文书
丧事答谢词
2015/01/05 职场文书
校园音乐节目广播稿
2015/08/19 职场文书
Matplotlib可视化之添加让统计图变得简单易懂的注释
2021/06/11 Python
详解MongoDB的条件查询和排序
2021/06/23 MongoDB