Vue+Openlayer使用modify修改要素的完整代碼
Vue+Openlayer使用modify修改要素,具體內(nèi)容如下所示:
import { Modify } from "ol/interaction";
- 可自動(dòng)捕捉
- 可修改點(diǎn)、線、面。不用自己聲明要修改的要素類型
直接修改要素

核心代碼:
// 修改要素核心代碼
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>
此外,可通過(guò)this.modify.setActive(false)來(lái)禁用modify對(duì)象,this.modify.getActive()獲取激活狀態(tài)
先選中要素,再修改要素

核心代碼:
注意:這里一定要用features屬性,不要用source!?。。?/p>
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中即可運(yùn)行
<!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>
到此這篇關(guān)于Vue+Openlayer使用modify修改要素的文章就介紹到這了,更多相關(guān)Vue Openlayer修改要素內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深入理解Vue父子組件生命周期執(zhí)行順序及鉤子函數(shù)
本文通過(guò)實(shí)例代碼給大家介紹了Vue父子組件生命周期執(zhí)行順序及鉤子函數(shù)的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-08-08
解決Vue項(xiàng)目中tff報(bào)錯(cuò)的問(wèn)題
這篇文章主要介紹了解決Vue項(xiàng)目中tff報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
vue.js實(shí)現(xiàn)含搜索的多種復(fù)選框(附源碼)
這篇文章主要給大家介紹了利用vue.js實(shí)現(xiàn)含搜索的多種復(fù)選框的相關(guān)資料,文中給出了簡(jiǎn)單的介紹,但提供了完整的實(shí)例源碼供大家下載學(xué)習(xí),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-03-03
vue改變對(duì)象或數(shù)組時(shí)的刷新機(jī)制的方法總結(jié)
這篇文章主要介紹了vue改變對(duì)象或數(shù)組時(shí)的刷新機(jī)制的方法總結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04
淺析vue-cli3配置webpack-bundle-analyzer插件【推薦】
小編最近為了優(yōu)化vue項(xiàng)目性能,需要使用webpack-bundle-analyzer插件來(lái)分析報(bào)文件,在網(wǎng)上沒(méi)有找到合適的,下面小編給大家寫出來(lái)一個(gè)供大家參考2019-10-10
Vue單文件組件開發(fā)實(shí)現(xiàn)過(guò)程詳解
這篇文章主要介紹了Vue單文件組件開發(fā)實(shí)現(xiàn)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
ant-design-vue 時(shí)間選擇器賦值默認(rèn)時(shí)間的操作
這篇文章主要介紹了ant-design-vue 時(shí)間選擇器賦值默認(rèn)時(shí)間的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨過(guò)來(lái)看看吧2020-10-10
vue+mockjs模擬數(shù)據(jù)實(shí)現(xiàn)前后端分離開發(fā)的實(shí)例代碼
本篇文章主要介紹了vue+mockjs模擬數(shù)據(jù)實(shí)現(xiàn)前后端分離開發(fā)的實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08

