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);
    }
相关推荐
GISer_Jing3 分钟前
从“工具应用”到“系统重构”:AI时代前端研发的范式转移与哲学思辨
前端·人工智能·学习
我家媳妇儿萌哒哒3 分钟前
Element ui el-dialog 在一个有滚动条的页面,打开一个弹框,完了再打开一个弹框后,滚动条可以滚动,怎么限制不能滚动。
前端·vue.js·ui
得想办法娶到那个女人5 分钟前
Vite + Vue 项目打包为 Electron 桌面应用 完整指南
前端·vue.js·electron
Sailing8 分钟前
🚀🚀CLI 为什么在 2025 年突然复兴?看懂 Agent、Skill、MCP、CLI 四层架构
前端·agent·ai编程
ZC跨境爬虫11 分钟前
Apple官网复刻第二阶段day_3:(还原苹果官网iPhone顶部标准文案区块,一次编写全局复用)
前端·css·ui·html·iphone
Momo__13 分钟前
CSS :has() 选择器:让父元素"看见"子元素的状态
前端·css
漫游的渔夫17 分钟前
前端开发者做 RAG:别只收集点赞点踩,用 6 个字段把反馈变成优化闭环
前端·人工智能·typescript
ponponon21 分钟前
openclaw 配置出错了,怎么重新再来?比如彻底卸载或者重新选一个AI模型
前端
Simon_52030 分钟前
Vue props传入function时的this指向问题_vue props function-CSDN博客
前端
写代码的皮筏艇31 分钟前
replace方法
前端·javascript