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+iview实现分页及查询功能
Nov 17 Vue.js
vue项目中openlayers绘制行政区划
Dec 24 Vue.js
vue3.0中友好使用antdv示例详解
Jan 05 Vue.js
vue编写简单的购物车功能
Jan 08 Vue.js
用vite搭建vue3应用的实现方法
Feb 22 Vue.js
vue实现可移动的悬浮按钮
Mar 04 Vue.js
Vue的列表之渲染,排序,过滤详解
Feb 24 Vue.js
vue使用wavesurfer.js解决音频可视化播放问题
Apr 04 Vue.js
vue的项目如何打包上线
Apr 13 Vue.js
vue项目打包后路由错误的解决方法
Apr 13 Vue.js
详解Vue3使用axios的配置教程
Apr 29 Vue.js
vue项目如何打包之项目打包优化(让打包的js文件变小)
Apr 30 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实现在服务器端调整图片大小的方法
2015/06/16 PHP
适用于初学者的简易PHP文件上传类
2015/10/29 PHP
PHP面向对象程序设计方法实例详解
2016/12/24 PHP
PHP实现防盗链的方法分析
2017/07/25 PHP
php实现微信公众平台发红包功能
2018/06/14 PHP
php7 错误处理机制修改实例分析
2020/05/25 PHP
flexigrid 类似ext grid的JS表格代码
2010/07/17 Javascript
Jquery之Ajax运用 学习运用篇
2011/09/26 Javascript
javascript两种function的定义介绍及区别说明
2013/05/02 Javascript
判断滚动条到底部的JS代码
2013/11/04 Javascript
setTimeout自动触发一个js的方法
2014/01/15 Javascript
Javascript控制input输入时间格式的方法
2015/01/28 Javascript
JQuery实现鼠标移动图片显示描述层的方法
2015/06/25 Javascript
浅谈Javascript实现继承的方法
2015/07/06 Javascript
JS函数的几种定义方式分析
2015/12/17 Javascript
jQuery基础知识点总结(DOM操作)
2016/06/01 Javascript
jquery 实现复选框的全选操作实例代码
2017/01/24 Javascript
JavaScript 自定义事件之我见
2017/09/25 Javascript
nodejs语言实现验证码生成功能的示例代码
2019/10/13 NodeJs
JavaScript实现打砖块游戏
2020/02/25 Javascript
Python编程之属性和方法实例详解
2015/05/19 Python
Using Django with GAE Python 后台抓取多个网站的页面全文
2016/02/17 Python
详解python中字典的循环遍历的两种方式
2017/02/07 Python
Python3网络爬虫之使用User Agent和代理IP隐藏身份
2017/11/23 Python
新手如何发布Python项目开源包过程详解
2019/07/11 Python
详解python中__name__的意义以及作用
2019/08/07 Python
Python爬取破解无线网络wifi密码过程解析
2019/09/17 Python
Python字典dict常用方法函数实例
2020/11/09 Python
python 实现简易的记事本
2020/11/30 Python
Monnier Frères美国官网:法国知名奢侈品网站
2016/11/22 全球购物
String是最基本的数据类型吗?
2013/06/13 面试题
2015年国庆节寄语
2015/08/17 职场文书
2015年评职称个人工作总结
2015/10/15 职场文书
如何使用Maxwell实时同步mysql数据
2021/04/08 MySQL
redis限流的实际应用
2021/04/24 Redis
Redis实现订单过期删除的方法步骤
2022/06/05 Redis