一、封装 zd_docEdit_Line.js
const LINE_STYLE = {
Color: 1, //子图的颜色号
LinStyleID: 0,
LinStyleID2: 19,
LinWidth: 0.1, //线宽
Xscale: 10,
Yscale: 10
}
const HOST = 'localhost';
const PORT = '6163';
class LineString {
/**
*
* @param {Array} position
* @param {array} attr
* @param {object} service {name,layerId,host,port}
*/
static add({
position,
attr,
service,
layer
}) {
/* 2.1 构建几何信息 */
var pointObj = []
for(let item of position){
pointObj.push(new Zondy.Object.Point2D(item[0],item[1]))
}
/* 构成折线的弧度 */
var gArc = new Zondy.Object.Arc(pointObj);
/* 构成线的折线 */
var gAnyLine = new Zondy.Object.AnyLine([gArc])
/* 设置线要素的几何信息 */
var gLine = new Zondy.Object.GLine(gAnyLine)
/* 设置要素的几何信息 */
var fGeom = new Zondy.Object.FeatureGeometry({
LinGeom: [gLine]
})
/* 2.2 设置而样式信息 */
//设置添加线要素的图形参数信息
var clineInfo = new Zondy.Object.CLineInfo(LINE_STYLE)
//设置要素的图形参数
var graphicInfo = new Zondy.Object.WebGraphicsInfo({
InfoType: 2, //线
LinInfo: clineInfo
})
/* 2.3 设置属性信息 */
var attValue = attr.map(item => item.value)
/* 2.4 构建要素类*/
//创建一个线要素
var newFeature = new Zondy.Object.Feature({
fGeom: fGeom,
GraphicInfo: graphicInfo,
AttValue: attValue
})
//设置要为线要素
newFeature.setFType(2)
/* 2.5 设置要素集 */
//创建一个要素数据集
var featureSet = new Zondy.Object.FeatureSet();
//创建属性结构设置对象
var cAttStruct = new Zondy.Object.CAttStruct({
FldName: attr.map(item => item.name),
FldNumber: attr.length,
FldType: attr.map(item => item.type)
})
//设置要素数据集的属性结构
featureSet.AttStruct = cAttStruct;
//将添加的线要素添加到属性数据集中
featureSet.addFeature(newFeature);
/* 2.6 调用地图编辑服务接口 */
//创建一个地图编辑对象
var editDocFeature = new Zondy.Service.EditDocFeature(service.name, service.layerId, {
ip: service.host || HOST,
port: service.port || PORT
})
editDocFeature.add(featureSet,this.onLineSuccess(layer))
}
static onLineSuccess(layer){
return function (data){
console.log(data);
if (data.succeed) {
alert("添加成功!")
layer.refresh() //重新加载地图文档
} else {
alert("添加失败")
}
}
}
}
二、使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./dist/include-openlayers-local.js"></script>
<script src="./js/TIAN.js"></script>
<script src="./js/Draw.js"></script>
<script src="./edit/es6_LineString.js"></script>
</head>
<body>
<button onclick="activeDraw()">激活画笔</button>
<div id="map_container">
</div>
<script>
var docLayer = new Zondy.Map.Doc('','city_area',{
ip:"localhost",
port:6163
})
var map = new ol.Map({
target:"map_container",
layers:[gaodeMapLayer],
view:new ol.View({
projection:'EPSG:4326',
center:[114.30,30.50],
zoom:4
})
})
map.addLayer(docLayer)
/* 1、创建画布 */
var source = new ol.source.Vector({wrapX:false})
var layer = new ol.layer.Vector({source})
map.addLayer(layer)
/* 2、激活画笔 */
let draw = null;
function activeDraw(){
draw = createDraw({
type:"LineString",
source,
callback:handleDraw
})
map.addInteraction(draw);
}
function handleDraw(e){
var position = e.feature.getGeometry().getCoordinates();
var layer = docLayer;
var attr = [
{name:"name",value:"随机",type:"string"},
{name:"length",value:2000,type:"int"}
]
var service = {name:"city_area",layerId:1}
LineString.add({
position,
layer,
attr,
service
})
}
</script>
</body>
</html>