欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Vue+Openlayer使用modify修改要素的完整代码

程序员文章站 2022-04-10 21:07:34
vue+openlayer使用modify修改要素,具体内容如下所示:import{modify}from"ol/interaction"; 可自动捕捉 可修改点、线、面。不用自己声明要修...

vue+openlayer使用modify修改要素,具体内容如下所示:

import { modify } from "ol/interaction";

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

直接修改要素 

 Vue+Openlayer使用modify修改要素的完整代码

核心代码:  

 // 修改要素核心代码
    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()获取激活状态

先选中要素,再修改要素

Vue+Openlayer使用modify修改要素的完整代码

核心代码:

注意:这里一定要用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>

ps:openlayers修改矢量要素

将以下代码放到demo下examples中即可运行

<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="apple-mobile-web-app-capable" content="yes">
<title>modify feature</title>
<link rel="stylesheet" href="../theme/default/style.css" rel="external nofollow"  rel="external nofollow"  type="text/css">
<link rel="stylesheet" href="style.css" rel="external nofollow"  rel="external nofollow"  type="text/css">
<style type="text/css">

</style>
<script src="../lib/openlayers.js"></script>
<script type="text/javascript">
var map, vectors, controls;
function init(){
map = new openlayers.map('map');
var wms = new openlayers.layer.wms( "openlayers wms",
"http://vmap0.tiles.osgeo.org/wms/vmap0?", {layers: 'basic'});
openlayers.feature.vector.style['default']['strokewidth'] = '2';

vectors = new openlayers.layer.vector("vector layer");

var geometry = openlayers.geometry.fromwkt(
'polygon((110 20,120 20,120 10,110 10,110 20),(112 17,118 18,118 16,112 15,112 17))'
);

vectors.addfeatures([new openlayers.feature.vector(geometry)]);

map.addlayers([wms, vectors]);
//画图形
controls = new openlayers.control.drawfeature(vectors,
openlayers.handler.polygon);

map.addcontrol(controls);
controls.activate();
map.setcenter(new openlayers.lonlat(110, 20), 3);
}

function update() {
// 修改
controls.deactivate();
controls = new openlayers.control.modifyfeature(vectors);
map.addcontrol(controls);
controls.activate();

}

function deactivate(){
controls.deactivate();
}

</script>
</head>
<body onload="init()">
<div id="map" class="smallmap"></div>
<div><input type = "button" value = "修改" onclick = "update()"/>
<input type = "button" value = "取消" onclick = "deactivate()"/>
</div>
</body>
</html>

到此这篇关于vue+openlayer使用modify修改要素的文章就介绍到这了,更多相关vue openlayer修改要素内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!