Vue+Openlayer使用modify修改要素

import { Modify } from "ol/interaction";

  1. 可自动捕捉  
  2. 可修改点、线、面。不用自己声明要修改的要素类型

直接修改要素 

 

核心代码:  

    // 修改要素核心代码
    modifyFeature() {
      this.modify = new Modify({
        source: this.lineStringLayer.getSource(),
      });
      this.map.addInteraction(this.modify);
    },

完整代码: 

<template>
  <div id="map" style="height: 100vh; width: 100vw"></div>
</template>
 
<script>
import "ol/ol.css";
import { Map, View, Feature } from "ol";
import { OSM, Vector as VectorSource } from "ol/source";
import { Vector as VectorLayer, Tile as TileLayer } from "ol/layer";

import { Point, LineString, Polygon } from "ol/geom";
import { Modify } from "ol/interaction";
export default {
  data() {
    return {
      map: {},
      lineStringLayer: {},
      modify: {},
    };
  },
  created() {},
  mounted() {
    this.initMap();
    this.addLayer();
    this.modifyFeature();
  },
  computed: {},
  methods: {
    initMap() {
      this.map = new Map({
        target: "map",
        layers: [
          new TileLayer({
            source: new OSM(),
          }),
        ],
        view: new View({
          projection: "EPSG:4326",
          center: [104.2979180563, 30.528298024],
          zoom: 18,
        }),
      });
    },
    addLayer() {
      this.lineStringLayer = new VectorLayer({
        source: new VectorSource(),
      });
      this.lineStringLayer.getSource().addFeature(
        new Feature({
          geometry: new LineString([
            [104.2979180563, 30.528298024],
            [104.2987389704, 30.527798338],
          ]),
        })
      );
      this.map.addLayer(this.lineStringLayer);
    },
    // 修改要素核心代码
    modifyFeature() {
      this.modify = new Modify({
        source: this.lineStringLayer.getSource(), //这里要用source
      });
      this.map.addInteraction(this.modify);
    },
  },
};
</script>

此外,可通过this.modify.setActive(false)来禁用modify对象,this.modify.getActive()获取激活状态

js封装修改功能

ModifyTool.js:

import "ol/ol.css";
import { Map, View, Feature } from "ol";
import { OSM, Vector as VectorSource, XYZ } from "ol/source";
import { Vector as VectorLayer, Tile as TileLayer } from "ol/layer";

import { Point, LineString, Polygon } from "ol/geom";
import { Modify } from "ol/interaction";
export default class ModifyTool {
    constructor({ domId_map }) {
        this.domId_map = domId_map
        this.map = null
        this.source = new VectorSource()
        this.lineStringLayer = null
        this.modify = null
    }
    initMap() {
        this.map = new Map({
            target: this.domId_map,
            layers: [
                new TileLayer({
                    source: new XYZ({
                        url:
                            "http://t3.tianditu.com/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=5a257cd2df1b3311723bd77b0de14baf",
                    }),
                    visible: true,
                    // name: "OSM",
                }),
            ],
            view: new View({
                projection: "EPSG:4326",
                center: [115, 39],
                zoom: 4,
            }),
        })

        this.addLayer()

    }
    addLayer() {
        this.source.addFeature(
            new Feature({
                geometry: new LineString([
                    [104.2979180563, 30.528298024],
                    [114.2987389704, 30.527798338],
                ]),
            })
        )
        this.lineStringLayer = new VectorLayer({
            source: this.source,
        });
        this.map.addLayer(this.lineStringLayer);
    }
    // 修改要素核心代码
    modifyFeature() {
        if (this.modify) {
            this.map.removeInteraction(this.modify);
        }
        this.modify = new Modify({
            source: this.lineStringLayer.getSource(), //这里要用source
        });
        this.map.addInteraction(this.modify);
    }
    // 取消修改
    cancelModify() {
        this.map.removeInteraction(this.modify);
    }
}

测试:

<template>
    <div class="main">
        <el-button @click="state.modify.modifyFeature()">开始修改</el-button>
        <el-button @click="state.modify.cancelModify()">取消修改</el-button>

        <div id="olContainer" style="height: 100vh; width: 100vw"></div>
    </div>

</template>
  
<script setup>
import { reactive } from 'vue';
import ModifyTool from './ModifyTool'

const state = reactive({
    modify: null
})
onMounted(() => {
    let modify = new ModifyTool({ domId_map: 'olContainer' })
    modify.initMap()

    state.modify = modify
})
</script>
<style lang="scss" scoped>
.main {
    position: relative;
    height: 100vh;
    width: 100vw;

    #olContainer {
        position: absolute;
        height: 100vh;
        width: 100vw;
    }

}
</style>


 

先选中要素,再修改要素

核心代码:

注意:这里一定要用features属性,不要用source!!!!

    modifyFeature() {
      this.modify = new Modify({
        //注意:这里一定要用features属性,不要用source!!!!
        features: this.select.getFeatures(),
      });
      this.map.addInteraction(this.modify);
    },

完整代码: 

<template>
  <div id="map" style="height: 100vh; width: 100vw"></div>
</template>
 
<script>
import "ol/ol.css";
import { Map, View, Feature } from "ol";
import { OSM, Vector as VectorSource, XYZ } from "ol/source";
import { Vector as VectorLayer, Tile as TileLayer } from "ol/layer";

import Select from "ol/interaction/Select";

import { Point, LineString, Polygon } from "ol/geom";
import { Modify } from "ol/interaction";
export default {
  data() {
    return {
      map: {},
      lineStringLayer: {},
      draw: {},
      modify: {},
      select: {},
    };
  },
  created() {},
  mounted() {
    this.initMap();
    this.pointerMove();
    this.addLayer();
    this.selectFeature();
    this.modifyFeature();
  },
  computed: {},
  methods: {
    initMap() {
      this.map = new Map({
        target: "map",
        layers: [
          new TileLayer({
            source: new OSM(),
          }),
        ],
        view: new View({
          projection: "EPSG:4326",
          center: [104.2979180563, 30.528298024],
          zoom: 18,
        }),
      });
    },
    pointerMove() {
      this.map.on("pointermove", (e) => {
        const isHover = this.map.hasFeatureAtPixel(e.pixel);
        this.map.getTargetElement().style.cursor = isHover ? "pointer" : "";
      });
    },
    addLayer() {
      this.lineStringLayer = new VectorLayer({
        source: new VectorSource(),
      });
      this.lineStringLayer.getSource().addFeature(
        new Feature({
          geometry: new LineString([
            [104.2979180563, 30.528298024],
            [104.2987389704, 30.527798338],
          ]),
        })
      );
      this.map.addLayer(this.lineStringLayer);
    },
    selectFeature() {
      this.select = new Select();
      this.map.addInteraction(this.select);
    },
    modifyFeature() {
      this.modify = new Modify({
        //注意:这里一定要用features属性,不要用source!!!!
        features: this.select.getFeatures(),
      });
      this.map.addInteraction(this.modify);
    },
  },
};
</script>

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在OpenLayers中添加要素,可以按照以下步骤进行操作: 1. 首先,确保已经引入所需的库文件。这可能包括Projection、Map、ImageLayer、View等等。[2] 2. 然后,在JavaScript代码中编写相关的代码。可以使用initMap()函数来初始化地图。在这个函数中,可以创建投影、定义图片范围、创建地图图层等。 3. 在创建地图的同时,可以添加要素图层。可以使用VectorLyr和VectorSource来创建一个矢量要素图层,并将其添加到地图的layers中。 4. 创建要素。可以使用不同的几何类型(例如Point、LineString、Polygon等)来创建要素对象。然后,可以为要素设置样式(例如填充颜色、边框颜色等)。 5. 将要素添加到要素图层中。可以使用addFeature()方法将要素添加到要素图层中。 下是一个示例代码片段,展示了如何添加要素OpenLayers地图中: ```javascript // 创建要素图层 var vectorSource = new VectorSource(); var vectorLayer = new VectorLyr({ source: vectorSource }); // 创建要素 var feature = new Feature({ geometry: new Point([0, 0]), // 设置要素的几何位置,这里使用了一个点 }); // 设置要素样式 var style = new Style({ image: new CircleStyle({ radius: 6, fill: new Fill({ color: 'red' }), stroke: new Stroke({ color: 'white', width: 2 }) }) }); feature.setStyle(style); // 将要素添加到图层中 vectorSource.addFeature(feature); // 将要素图层添加到地图中 map.addLayer(vectorLayer); ``` 这样,就可以在OpenLayers地图中成功添加要素了。请根据你的实际需求进行相应的调整和修改

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值