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.js 遇到的那些坑
Feb 02 Vue.js
vue3.0 自适应不同分辨率电脑的操作
Feb 06 Vue.js
vue使用节流函数的踩坑实例指南
May 20 Vue.js
Vue鼠标滚轮滚动切换路由效果的实现方法
Aug 04 Vue.js
详解Vue slot插槽
Nov 20 Vue.js
Vue的过滤器你真了解吗
Feb 24 Vue.js
VUE使用draggable实现组件拖拽
Apr 06 Vue.js
vue封装数字翻牌器
Apr 20 Vue.js
vue二维数组循环嵌套方式 循环数组、循环嵌套数组
Apr 24 Vue.js
vue里使用create, mounted调用方法
Apr 26 Vue.js
vue递归实现树形组件
Jul 15 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
Linux编译升级php的详细方法
2013/11/04 PHP
使用YUI+Ant 实现JS CSS压缩
2014/09/02 PHP
是 WordPress 让 PHP 更流行了 而不是框架
2016/02/03 PHP
curl 出现错误的调试方法(必看)
2017/02/13 PHP
IE和Firefox下javascript的兼容写法小结
2008/12/10 Javascript
JS验证控制输入中英文字节长度(input、textarea等)具体实例
2013/06/21 Javascript
jquery表单验证框架提供的身份证验证方法(示例代码)
2013/12/27 Javascript
jQuery插件animateSlide制作多点滑动幻灯片
2015/06/11 Javascript
jQuery Ajax页面局部加载方法汇总
2016/06/02 Javascript
js实现文字截断功能
2016/09/14 Javascript
JavaScript对象封装的简单实现方法(3种方法)
2017/01/03 Javascript
详解JavaScript中关于this指向的4种情况
2019/04/18 Javascript
Vue实现兄弟组件间的联动效果
2020/01/21 Javascript
仿照Element-ui实现一个简易的$message方法
2020/09/14 Javascript
初学python数组的处理代码
2011/01/04 Python
python在多玩图片上下载妹子图的实现代码
2013/08/13 Python
python多线程操作实例
2014/11/21 Python
在python3环境下的Django中使用MySQL数据库的实例
2017/08/29 Python
对Python中list的倒序索引和切片实例讲解
2018/11/15 Python
前端制作动画的几种方式(css3,js)
2016/12/12 HTML / CSS
html5 Canvas画图教程(2)—画直线与设置线条的样式如颜色/端点/交汇点
2013/01/09 HTML / CSS
马克华菲官方商城:Mark Fairwhale
2016/09/04 全球购物
Willer台湾:日本高速巴士/夜行巴士预约
2017/07/09 全球购物
Keds加拿大官网:购买帆布运动鞋和皮鞋
2019/09/26 全球购物
俄罗斯购买内衣网站:Trusiki
2020/08/22 全球购物
如何唤起类中的一个方法
2013/11/29 面试题
大学生四个方面的自我评价
2013/09/19 职场文书
干部个人对照检查材料
2014/08/25 职场文书
推广普通话共筑中国梦演讲稿
2014/09/21 职场文书
2014年监理工作总结范文
2014/11/17 职场文书
布达拉宫的导游词
2015/02/02 职场文书
总经理岗位职责
2015/02/04 职场文书
校园安全学习心得体会
2016/01/18 职场文书
《好妈妈胜过好老师》:每个孩子的优秀都是有源头的
2020/01/03 职场文书
厉害!这是Redis可视化工具最全的横向评测
2021/07/15 Redis
漫画《催眠麦克风-Dawn Of Divisions》第二卷PV公开
2022/04/05 日漫