Openlayers6 要素的绘制、编辑、GeoJson格式输出

Openlayers6 要素的绘制、编辑、GeoJson格式输出

1. 问题描述

在使用openlayers开发时,遇到过交互绘制、编辑要素,有时还需要以geojson格式存储要素,今天就基于Vue写一个demo简单粗暴地记录一下这三个功能实现。各位有需要的GISer可以借鉴,如有指正和建议欢迎给评论哦~~

在这里插入图片描述

2.代码实现

<template>
  <!-- 绘制、编辑、输出GeoJson数据 -->
  <div id="map" style="width:100%;height:100%">
    <div class="btns">
      <button @click="drawFeature">绘制</button>
      <button @click="editorFeature" v-text="editorBtnText"></button>
      <button @click="outputJson">输出Json</button>
    </div>
  </div>
</template>

<script>
import "ol/ol.css";
import Map from "ol/Map";
import View from "ol/View";
import { Vector as VectorSource, OSM } from "ol/source";
import { Tile as TileLayer, Vector as VectorLayer } from "ol/layer";
import { GeoJSON } from "ol/format";
import { Draw, Modify, Select } from "ol/interaction";
export default {
  data() {
    return {
      map: null,
      vectorLayer: null,
      vectorSource: null,
      draw: null,
      select: null,
      modify: null,
      editorBtnText: "编辑"
    };
  },
  mounted() {
    // 底图
    var baseLayer = new TileLayer({
      source: new OSM()
    });
    // 矢量图层源
    this.vectorSource = new VectorSource({
      wrapX: false
    });
    // 矢量图层
    this.vectorLayer = new VectorLayer({
      source: this.vectorSource
    });

    this.map = new Map({
      layers: [baseLayer, this.vectorLayer],
      target: "map",
      view: new View({
        projection: "EPSG:4326",
        center: [104.06531800244139, 30.65852484539117],
        zoom: 10
      })
    });
  },
  methods: {
  
    drawFeature() {
      this.draw = new Draw({
        source: this.vectorSource,
        type: "Polygon"
      });
      this.map.addInteraction(this.draw);
	  // 绘制完成
      this.draw.on("drawend", e => {
        this.map.removeInteraction(this.draw);
        this.draw = null;
      });
    },
    
    editorFeature() {
      if (this.editorBtnText == "编辑") {
        this.editorBtnText = "完成编辑";
        // 要素选择组件
        this.select = new Select({
          wrapX: false
        });
        // 要素编辑
        this.modify = new Modify({
          features: this.select.getFeatures()
        });

        // 编辑完成后
        this.modify.on("modifyend", function(e) {
          console.log("编辑后的要素:", e.features);
        });

        this.map.addInteraction(this.select);
        this.map.addInteraction(this.modify);
      } else {
        this.editorBtnText = "编辑";
        this.map.removeInteraction(this.select);
        this.map.removeInteraction(this.modify);
        this.select = null;
        this.modify = null;
      }
    },
    
    // 输出矢量图层要素为GeoJson数据
    outputJson() {
      let features = this.vectorSource.getFeatures();
      let jsonObj = new GeoJSON().writeFeatures(features);
      console.log("->GeoJson格式数据:", jsonObj);
    }
  }
};
</script>

<style scoped>
#map {
  width: 100%;
  height: 100%;
  position: relative;
}
.btns {
  position: absolute;
  z-index: 999;
  left: 50%;
  top: 20px;
  transform: translate(-50%);
}
.btns button {
  display: inline-block;
  text-decoration: none;
  text-align: center;
  height: 30px;
  width: 90px;
}
</style>
  • 9
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
OpenLayers是一种用于Web地图应用程序的JavaScript库,它提供了丰富的功能和工具来实现地图的可视化展示和交互。而GeoJSON是一种基于JavaScript对象表示法(JSON)的地理空间数据格式,它可以用来描述地理要素和属性。 要使用OpenLayers根据GeoJSON绘制图形,我们可以按照以下步骤进行: 1. 引入OpenLayers库和相关的样式表文件到我们的HTML页面中。 2. 创建一个包含地图的容器元素。例如,可以在HTML中创建一个div元素,并给它一个唯一的id作为标识。 3. 使用JavaScript代码来初始化地图。首先,我们需要创建一个地图对象,并指定它的目标容器为前面创建的容器元素。然后,我们可以设置地图的视图和图层。 4. 创建一个矢量图层,用于显示我们的GeoJSON数据。我们可以使用OpenLayers提供的ol.layer.Vector类来创建一个矢量图层。 5. 通过Ajax获取或直接赋值一个包含GeoJSON数据的JSON对象。 6. 创建一个数据源对象,用于加载GeoJSON数据。我们可以使用OpenLayers提供的ol.source.Vector类来创建一个数据源对象,并传入我们的GeoJSON数据。 7. 创建一个要素对象,将其添加到数据源对象中,然后将数据源对象添加到矢量图层中。 8. 将矢量图层添加到地图中。 通过以上步骤,我们就可以使用OpenLayers根据GeoJSON数据绘制图形了。这些图形可以是点、线或面等地理要素。在绘制完成后,我们还可以根据需要设置图形的样式、交互操作、弹出窗口等其他功能。 值得注意的是,OpenLayers还提供了许多其他功能和方法,可以帮助我们对地图进行更多的操作和自定义。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值