1、前言
在OpenLayers
中,要素的编辑主要使用ol.interaction.Select
和ol.interaction.Modify
配合实现,下面开始介绍。
2、编辑要素
编辑功能的实现很简答,ol.interaction.Select
负责选择要素,ol.interaction.Modify
对被选择的要素进行编辑,代码如下:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>编辑要素</title>
<link href="lib/ol/ol.css" rel="stylesheet" />
<script src="lib/ol/ol.js"></script>
</head>
<body>
<div id="map" style="width: 800px; height: 800px;"></div>
<script>
// 创建图层
var layer = new ol.layer.Vector({
source: new ol.source.Vector({
features: [
new ol.Feature({
geometry: new ol.geom.Point([119.4, 29.6]),
}),
new ol.Feature({
geometry: new ol.geom.LineString([
[119.0, 29.4],
[119.2, 29.3],
[119.4, 29.5]
])
}),
new ol.Feature({
geometry: new ol.geom.Polygon([[
[119.4, 29.0],
[119.6, 29.0],
[119.5, 29.2],
[119.4, 29.0]
]])
})
]
}),
style: new ol.style.Style({
image: new ol.style.Circle({
radius: 30,
fill: new ol.style.Fill({
color: 'red'
})
}),
stroke: new ol.style.Stroke({
color: 'blue',
width: 2
}),
fill: new ol.style.Fill({
color: 'yellow'
})
})
});
// 创建地图
var map = new ol.Map({
target: 'map',
layers: [
layer
],
view: new ol.View({
projection: 'EPSG:4326',
center: [119.2, 29.2],
zoom: 10
})
});
// 创建选择工具
var select = new ol.interaction.Select({
condition: ol.events.condition.singleClick,
style: new ol.style.Style({
image: new ol.style.Circle({
radius: 30,
fill: new ol.style.Fill({
color: 'red'
})
}),
stroke: new ol.style.Stroke({
color: 'blue',
width: 2
}),
fill: new ol.style.Fill({
color: 'yellow'
})
}),
layers: [
layer
]
});
// 创建修改工具
var modify = new ol.interaction.Modify({
features: select.getFeatures()
});
// 添加交互工具
map.addInteraction(select);
map.addInteraction(modify);
</script>
</body>
</html>
运行结果如下图所示:
3、要素编辑后的处理
在对要素进行拖动或者拉伸等操作后,其坐标肯定会随之发生改变,如果要获取编辑后的要素坐标,则需要监听modifyend
事件,代码如下:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>编辑要素</title>
<link href="lib/ol/ol.css" rel="stylesheet" />
<script src="lib/ol/ol.js"></script>
</head>
<body>
<div id="map" style="width: 800px; height: 800px;"></div>
<script>
// 创建图层
var layer = new ol.layer.Vector({
source: new ol.source.Vector({
features: [
new ol.Feature({
geometry: new ol.geom.Point([119.4, 29.6]),
}),
new ol.Feature({
geometry: new ol.geom.LineString([
[119.0, 29.4],
[119.2, 29.3],
[119.4, 29.5]
])
}),
new ol.Feature({
geometry: new ol.geom.Polygon([[
[119.4, 29.0],
[119.6, 29.0],
[119.5, 29.2],
[119.4, 29.0]
]])
})
]
}),
style: new ol.style.Style({
image: new ol.style.Circle({
radius: 30,
fill: new ol.style.Fill({
color: 'red'
})
}),
stroke: new ol.style.Stroke({
color: 'blue',
width: 2
}),
fill: new ol.style.Fill({
color: 'yellow'
})
})
});
// 创建地图
var map = new ol.Map({
target: 'map',
layers: [
layer
],
view: new ol.View({
projection: 'EPSG:4326',
center: [119.2, 29.2],
zoom: 10
})
});
// 创建选择工具
var select = new ol.interaction.Select({
condition: ol.events.condition.singleClick,
style: new ol.style.Style({
image: new ol.style.Circle({
radius: 30,
fill: new ol.style.Fill({
color: 'red'
})
}),
stroke: new ol.style.Stroke({
color: 'blue',
width: 2
}),
fill: new ol.style.Fill({
color: 'yellow'
})
}),
layers: [
layer
]
});
// 创建修改工具
var modify = new ol.interaction.Modify({
features: select.getFeatures()
});
// 监听modifyend事件,获取要素修改后的坐标
modify.on('modifyend', function (e) {
var features = e.features;
if (features.getLength() > 0) {
var feature = features.item(0);
var geometry = feature.getGeometry();
var coordinates = geometry.getCoordinates();
// 点
if (geometry instanceof ol.geom.Point) {
console.log('更新后的点坐标:[' + coordinates + ']');
}
// 线
if (geometry instanceof ol.geom.LineString) {
var msg = [];
for (var i = 0; i < coordinates.length; i++) {
msg.push('[' + coordinates[i] + ']');
}
console.log('更新后的线坐标:' + msg.join('、'));
msg = [];
}
// 面
if (geometry instanceof ol.geom.Polygon) {
var msg = [];
for (var i = 0; i < coordinates.length; i++) {
var coordinate = coordinates[i];
for (var j = 0; j < coordinate.length; j++) {
msg.push('[' + coordinate[j] + ']');
}
}
console.log('更新后的面坐标:' + msg.join('、'));
msg = [];
}
}
});
// 添加交互工具
map.addInteraction(select);
map.addInteraction(modify);
</script>
</body>
</html>
运行结果如下图所示: