openLayers--绘制多边形、获取视图的中心点、获取当前地图等级、设置地图等级

openLayers绘制多边形、获取视图中心点

前言

上一篇文章在vue项目中绘制了openlayers绘制了地图和标记点,本篇文章讲解openlayers绘制多边形

通过LineString来进行绘制、获取视图的中心位置、获取当前地图等级、设置地图等级
openLayers演示网址

效果图



1、导入LineString

import { Point, LineString } from "ol/geom";

2、创建添加多边形

java 复制代码
// 添加多边形
    addExtent (extent) {
      if (this.vectorSource) {
        // this.vectorSource.clear()
      } else {
        //矢量标注的数据源
        this.vectorSource = new VectorSource({
          features: []
        })
        // //矢量标注图层
        this.vectorLayer = new VectorLayer({
          source: this.vectorSource
        });
        this.map.addLayer(this.vectorLayer);
      }

      // // 创建要素,设置其样式
      var newPolygon = new Feature({
        geometry: new LineString([
        [110.3014, 14.82],
        [112.79, 14.82],
        [114.6636, 18.2977],
        [111.687, 18.897],
        [110.3014, 14.82],
      ])
      });

      newPolygon.setStyle(this.createfeature(newPolygon));
      this.vectorSource.addFeature(newPolygon);
    },

3、定义多变形样式

java 复制代码
// 定义多边形
    createfeature () {
      return new Style({
        fill: new Fill({
          color: 'rgba(1, 210, 241, 0.2)'
        }),
        stroke: new Stroke({
          color: 'rgba(255, 0, 0)',
          width: 4,
        }),
      })
    },

4、获取当前视图的中心点

javascript 复制代码
	// 获取范围
    getExtent () {
      const size = this.map.getSize();
      const extent = this.map.getView().calculateExtent(size);
      return extent
    },

    // 获取地图视野中心点
    getCenter () {
      const center = getCenter(this.getExtent())
      this.addVectorLabel(center)
    },

5、获取当前视图等级

javascript 复制代码
// 获取当前的视图等级
    getZoom(){
       alert(this.map.getView().getZoom());
    }

6、设置地图等级

javascript 复制代码
	// 设置当前缩放等级
    setZoom(){
      this.map.getView().setZoom(10);
    }
相关推荐
qq_386322693 小时前
华为网路设备学习-21 IGP路由专题-路由过滤(filter-policy)
前端·网络·学习
a濯8 小时前
element plus el-table多选框跨页多选保留
javascript·vue.js
蓝婷儿8 小时前
前端面试每日三题 - Day 32
前端·面试·职场和发展
星空寻流年9 小时前
CSS3(BFC)
前端·microsoft·css3
九月TTS9 小时前
开源分享:TTS-Web-Vue系列:Vue3实现固定顶部与吸顶模式组件
前端·vue.js·开源
CodeCraft Studio10 小时前
数据透视表控件DHTMLX Pivot v2.1发布,新增HTML 模板、增强样式等多个功能
前端·javascript·ui·甘特图
一把年纪学编程10 小时前
【牛马技巧】word统计每一段的字数接近“字数统计”
前端·数据库·word
llc的足迹10 小时前
el-menu 折叠后小箭头不会消失
前端·javascript·vue.js
九月TTS10 小时前
TTS-Web-Vue系列:移动端侧边栏与响应式布局深度优化
前端·javascript·vue.js
曾经的你d10 小时前
【electron+vue】常见功能之——调用打开/关闭系统软键盘,解决打包后键盘无法关闭问题
vue.js·electron·计算机外设