地图技术知识:openlayers学习之绘制点、线、多边形、圆(鼠标绘制)

该博客详细介绍了如何使用OpenLayers库在Web应用中创建地图,并实现地图上的几何对象绘制(点、线、多边形、圆)及交互功能,包括选择类型、获取添加的元素等操作。通过示例代码展示了地图初始化、图层添加、单击事件监听以及绘制工具的添加和管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

组件代码

<template>
  <div>
    <div>
      <span>Geometry type &nbsp;&nbsp;&nbsp;</span>
      <select v-model="typeSelected"
              @change="getTypeSelected">
        <option value="Point"></option>
        <option value="LineString">线</option>
        <option value="Polygon">多边形</option>
        <option value="Circle"></option>
        <option value="None"></option>
      </select>
      <button @click="getAllFeatures">获取添加的元素</button>
    </div>
    <div id="map"
         ref="rootmap"></div>
  </div>
</template>

<script>
import "ol/ol.css";
import { Map, View } from "ol";
import { Vector as LayerVec, Image } from 'ol/layer'
import { Vector as SourceVec, ImageWMS } from "ol/source";
import { Style, Stroke } from "ol/style";
import Draw from "ol/interaction/Draw";
import Projection from 'ol/proj/Projection'
export default {
  data () {
    return {
      typeSelected: 'Point',
      map: null,
      vectorBox: null,
      drawLayer: null,
      draw: null
    };
  },
  mounted () {
    let that = this
    let mapUrl = "http://111.229.215.211:8080/geoserver/sxmap/wms"
    let mapName = "sxmap:hfshape"
    let myCode = "EPSG:404000"
    let myUnits = 'degrees'
    //wms的边界范围
    let extent = [-5783.940779486906, 2420.684125662136, -3999.448596040022, 5088.623484956386]
    //wms作底图
    let wmsLayer = [
      new Image({
        source: new ImageWMS({
          ratio: 1,
          url: mapUrl,
          params: {
            'FORMAT': 'image/png',
            'VERSION': '1.1.1',
            "STYLES": '',
            "LAYERS": mapName,
            "exceptions": 'application/vnd.ogc.se_inimage',
          }
        })
      })
    ];
    // 坐标系
    let projection = new Projection({
      code: myCode,
      units: myUnits,
      global: false
    })
    //定义地图对象
    that.map = new Map({
      layers: wmsLayer,
      target: 'map',
      view: new View({
        projection,
        zoom: 4,
        maxZoom: 20,
      }),
    });
    // 根据范围让地图元素居中显示  重点
    that.map.getView().fit(extent, that.map.getSize());
    // 监听singleclick事件
    that.map.on('singleclick', function (e) {
      console.log("coordinate", e.coordinate)
    })
    //创建矢量容器
    let vectorBox = new SourceVec({
      features: []
    })
    that.vectorBox = vectorBox
    //创建图层
    let drawLayer = new LayerVec({
      source: vectorBox
    })
    that.drawLayer = drawLayer
    //将图层添加到地图上
    that.map.addLayer(drawLayer)
    // 添加绘制工具
    this.addDraw()
  },
  methods: {
    getTypeSelected () {
      this.draw && this.map.removeInteraction(this.draw);
      //再根据typeSelect的值重新设置新的Interaction
      this.addDraw();
    },
    addDraw () {
      let value = this.typeSelected;
      if (value !== 'None') {
        this.draw = new Draw({
          source: this.drawLayer.getSource(),
          type: this.typeSelected,
          style: new Style({
            stroke: new Stroke({
              color: 'blue',
              width: 3
            })
          })
        });
        this.map.addInteraction(this.draw);
      }
    },
    getAllFeatures () {
      let that = this
      let Features = that.vectorBox.getFeatures()
      console.log('Features', Features)
      if (Features && Features.length) {
        console.log(Features[0].getGeometry().flatCoordinates)
      }
    }
  }
};
</script>

<style lang="less" scoped>
#map {
  height: 500px;
}
/*隐藏ol的一些自带元素*/
.ol-attribution,
.ol-zoom {
  display: none;
}
</style>

效果图

在这里插入图片描述

参考文档

参考文档01
参考文档02

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值