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);
    }
相关推荐
不想有bug的小菜鸟几秒前
vue3使用iframe全屏展示pdf效果
前端·pdf
m0_748238631 分钟前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
u0100559602 分钟前
前端代理,解决跨域问题讲解
前端
quitv7 分钟前
react脚手架配置别名
前端·javascript·react.js
m0_5287238116 分钟前
前端如何进行性能优化
前端·性能优化
化作繁星17 分钟前
在 Vue 3 中,如何缓存和复用动态组件
前端·vue.js·缓存
一粒沙-37 分钟前
iOS 将GIF图分享至微信
前端·ios
graywen41 分钟前
从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
前端
一只小姜丝3321 小时前
解决各大浏览器中http地址无权限调用麦克风摄像头问题
网络·vue.js·网络协议·http
Gazer_S2 小时前
【现代前端框架中本地图片资源的处理方案】
前端·javascript·chrome·缓存·前端框架