一、封装
给Point添加一个query方法,进行查询
/* es6封装添加点 */const POINT_STYLE = {Angle: 0,Color: 2, //子图的颜色Space: 0,SymHeight: 5, //点的高度SymID: 31,SymWidth: 5 //点的宽度}const HOST = 'localhost';const PORT = 6163;/*** @param {object} service {name,layerId,host,port}*/class Point {.../* 查询 */static query({position,service}){//1、创建一个用于查询的点形状var pointObj = new Zondy.Object.Point2D(position[0], position[1]);//设置查询点的搜索半径pointObj.nearDis = 1//2、初始化查询结构对象,告诉服务端查询结果中应该包含哪些信息var queryStruct = new Zondy.Service.QueryFeatureStruct()//是否包含几何图形信息queryStruct.IncludeGeometry = true;//是否包含属性信息queryStruct.IncludeAttribute = true;//是否包含图形显示参数queryStruct.IncludeWebGraphic = true;//3、创建查询规则var rule = new Zondy.Service.QueryFeatureRule({//是否将要素的可见性计算在内EnableDisplayCondition: false,//是否完全包含MustInside: false,//是否仅比较要素的外包矩形CompareRectOnly: false,//是否相交Intersect: true})//4、实例化查询参数对象var queryParam = new Zondy.Service.QueryParameter({geometry: pointObj,resultFormat: 'json',struct: queryStruct,rule: rule})//5、实例化地图文档查询服务对象var queryService = new Zondy.Service.QueryDocFeature(queryParam,service.name,service.layerId, {ip: service.host || HOST,port: service.port || PORT //访问IGServer的端口号, .net为6163,Java为8089})queryService.query(this.querySuccess, this.queryError)}static querySuccess(result){var format = new Zondy.Format.PolygonJSON();//将MapGIS要素JSON反序列化为ol.Feature类型数组var features = format.read(result)console.log(features)}static queryError(e){console.log(e);}}
二、例子
<body><button onclick="addDraw()">添加画笔</button><button onclick="exit()">退出</button><div id="map_container"></div><script>var docLayer = new Zondy.Map.Doc('', 'smart_city', {ip: "localhost",port: 6163})var map = new ol.Map({target: "map_container",layers: [gaodeMapLayer, docLayer],view: new ol.View({projection: 'EPSG:4326',center: [114.30, 30.50],zoom: 4})})/* 1、添加交互式画笔,获取坐标 */// 1-1、创建画布var source = new ol.source.Vector({wrapX:false});var layer = new ol.layer.Vector({source})map.addLayer(layer);let draw;function addDraw(){draw =createDraw({type:"Point",source,callback:handleDraw})map.addInteraction(draw);}function handleDraw(e){var position = e.feature.getGeometry().getCoordinates();Point.query({position,service:{name:"smart_city",layerId:0}})}function exit(){map.removeInteraction(draw);draw = null;}</script></body></html>
