业务
1、通过交互式的画笔获取点的坐标
2、调用编辑服务,添加点
一、draw.js
封装的交互式绘制
/**
*
*
* @param {Function} callback
* @param {Point,LineString,Circle,Polygon,Square,Rectangle} type
*/
function createDraw({
source,
type = "Point",
callback
}) {
let draw = null;
let geometryFunction = null;
let maxPoints = 0;
if (type == "Square") {
type = "Circle";
geometryFunction = ol.interaction.Draw.createRegularPolygon(4)
} else if (type == "Rectangle") {
type = "LineString";
geometryFunction = function (coordinates, geometry) {
if (!geometry) {
//多边形
geometry = new ol.geom.Polygon(null)
}
var start = coordinates[0]
var end = coordinates[1]
geometry.setCoordinates([
[start, [start[0], end[1]], end, [end[0], start[1]], start],
])
return geometry
}
maxPoints = 2;
}
draw = new ol.interaction.Draw({
source,
type,
geometryFunction,
maxPoints
})
callback && draw.on("drawend",callback)
return draw;
}
二、addPoint.js
封装点—> 编辑地图
const POINT_STYLE = {
Angle: 0,
Color: 2, //子图的颜色
Space: 0,
SymHeight: 5, //点的高度
SymID: 31,
SymWidth: 5 //点的宽度
}
/**
*
* @param {array} position 坐标的位置
* @param {string} layer 图层
* @param {array} attr [{name,value,type}]
* @param {object} service {name,layerId}
*/
function addPoint({
position,
layer,
attr,
service
}) {
/* 2.1构建几何信息 */
/* 创建一个点形状,描述点形状的几何信息 */
var gpoint = new Zondy.Object.GPoint(position[0], position[1])
/* 设置当前点要素的几何信息 */
var fGeom = new Zondy.Object.FeatureGeometry({
PntGeom: [gpoint]
})
/* 2.2 设置样式信息 */
var pointInfo = new Zondy.Object.CPointInfo(POINT_STYLE)
/* 设置当点要素的图形参数信息 */
var webGraphicInfo = new Zondy.Object.WebGraphicsInfo({
InfoType: 1, //点
PntInfo: pointInfo
})
/* 2.3 设置属性信息 */
/* 设置添加点要素的属性信息 */
var attValue = attr.map(item => item.value)
/* 2.4 构建要素对象 */
var feature = new Zondy.Object.Feature({
fGeom: fGeom, //坐标--几何信息
GraphicInfo: webGraphicInfo, //样式信息
AttValue: attValue //属性
})
/* 设置要素为点要素
点 -->1
线 -->2
面 -->3
*/
feature.setFType(1)
/* 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(feature)
/* 2.6 调用编辑服务接口 */
/*
创建一个编辑服务类
第一个参数:服务的名称 第二参数:图层的名称
*/
var editService = new Zondy.Service.EditDocFeature(service.name, service.layerId, {
ip: 'localhost',
port: '6163' //访问IGServer的端口号, .net为6163,Java为8089
})
//执行添加点要素功能
editService.add(featureSet, onPntSuccess(layer))
}
function onPntSuccess(layer) {
return function (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/addPoint.js"></script>
</head>
<body>
<button onclick="add()">添加画笔</button>
<button onclick="removeDraw()">移除画笔</button>
<div id="map_container">
</div>
<script>
var map = new ol.Map({
target:"map_container",
layers:[gaodeMapLayer],
view:new ol.View({
projection:'EPSG:4326',
center:[114.30,30.50],
zoom:4
})
})
var docLayer = new Zondy.Map.Doc('','city_area',{
ip:'localhost',
port:6163
})
map.addLayer(docLayer)
/* 1、创建画布 */
var source = null;
var layer = new ol.layer.Vector({})
map.addLayer(layer);
/* 2、添加画笔 */
let draw = null;
function add(){
if(source == null){
source = new ol.source.Vector({wrapX:false})
layer.setSource(source);
}
draw = createDraw({
source,
type:"Point",
callback:handleDraw
})
map.addInteraction(draw);
}
function removeDraw(){
source = null;
layer.setSource(source);
map.removeInteraction(draw);
}
function handleDraw(e){
// console.log(e.feature.getGeometry().getCoordinates());
// console.log(e.target.sketchCoords_)
var position = e.feature.getGeometry().getCoordinates();
console.log(position)
var attr = [{name:"name",value:"武汉",type:"string"}]
var service = {name:"city_area",layerId:2}
addPoint({
position,
layer:docLayer,
attr,
service
})
}
</script>
</body>
</html>