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项目如何引入bootstrap、elementUI、echarts
Nov 26 Vue.js
vue动态合并单元格并添加小计合计功能示例
Nov 26 Vue.js
Vue实现图书管理小案例
Dec 03 Vue.js
vue祖孙组件之间的数据传递案例
Dec 07 Vue.js
vue-cli4.0多环境配置变量与模式详解
Dec 30 Vue.js
Vue常用API、高级API的相关总结
Feb 02 Vue.js
Vue+Bootstrap实现简易学生管理系统
Feb 09 Vue.js
vue+flask实现视频合成功能(拖拽上传)
Mar 04 Vue.js
Vue2.0搭建脚手架
Mar 13 Vue.js
Vue组件更新数据v-model不生效的解决
Apr 02 Vue.js
vue实现在data里引入相对路径
Jun 05 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
Protoss兵种介绍
2020/03/14 星际争霸
php自动注册登录验证机制实现代码
2011/12/20 PHP
屏蔽机器人从你的网站搜取email地址的php代码
2012/11/14 PHP
PHP fclose函数用法总结
2019/02/15 PHP
php中关于换行的实例写法
2019/09/26 PHP
js 目录列举函数
2008/11/06 Javascript
JQuery 插件模板 制作jquery插件的朋友可以参考下
2010/03/17 Javascript
Ext 今日学习总结
2010/09/19 Javascript
文档对象模型DOM通俗讲解
2013/11/01 Javascript
jQuery链使用指南
2015/01/20 Javascript
jquery实现弹出层效果实例
2015/05/19 Javascript
jQuery热气球动画半透明背景的后台登录界面代码分享
2015/08/28 Javascript
微信小程序 天气预报开发实例代码源码
2017/01/20 Javascript
解决bootstrap中使用modal加载kindeditor时弹出层文本框不能输入的问题
2017/06/05 Javascript
深入理解ES6之数据解构的用法
2018/01/13 Javascript
Angular学习笔记之集成三方UI框架、控件的示例
2018/03/23 Javascript
layDate日期控件使用方法详解
2018/11/15 Javascript
使用Javascript简单计算器
2018/11/17 Javascript
微信小程序sessionid不一致问题解决
2019/08/30 Javascript
uni-app 自定义底部导航栏的实现
2020/12/11 Javascript
python创建临时文件夹的方法
2015/07/06 Python
Python中 传递值 和 传递引用 的区别解析
2018/02/22 Python
TensorFlow实现RNN循环神经网络
2018/02/28 Python
django 通过URL访问上传的文件方法
2019/07/28 Python
Python爬虫运用正则表达式的方法和优缺点
2019/08/25 Python
Python序列化pickle模块使用详解
2020/03/05 Python
pycharm 关闭search everywhere的解决操作
2021/01/15 Python
英国DVD和蓝光碟片购买网站:Zoom.co.uk(电影和电视)
2019/09/23 全球购物
会计专业个人求职信范文
2014/01/08 职场文书
写给老师的表扬信
2014/01/21 职场文书
项目施工员岗位职责
2014/03/09 职场文书
心理咨询专业自荐信
2014/07/07 职场文书
民主评议教师党员自我评价
2015/03/04 职场文书
入党介绍人考察意见
2015/06/01 职场文书
《别在吃苦的年纪选择安逸》读后感3篇
2019/11/30 职场文书
JavaWeb 入门:Hello Servlet
2021/07/16 Java/Android