一、draw.js

  1. /**
  2. * 根据类型创建画笔
  3. * @param {object} source ol.source 数据源对象
  4. * @param {string} type 画笔的类型 ['Point', 'LineString', 'Circle', 'Polygon', 'Square', 'Box']
  5. * @param {function} callback 绘制完成的回调函数
  6. * @returns {ol.interaction.Draw} draw
  7. */
  8. function createDraw({source, type="Point", callback}) {
  9. let draw = null
  10. let geometryFunction = null
  11. let maxPoints = 0
  12. if (type == 'Square') {
  13. type = 'Circle'
  14. geometryFunction = ol.interaction.Draw.createRegularPolygon(4)
  15. } else if (type == 'Box') {
  16. type = 'LineString'
  17. geometryFunction = function (coordinates, geometry) {
  18. if (!geometry) {
  19. //多边形
  20. geometry = new ol.geom.Polygon(null)
  21. }
  22. var start = coordinates[0]
  23. var end = coordinates[1]
  24. geometry.setCoordinates([
  25. [
  26. [start[0], start[1]],
  27. [start[0], end[1]],
  28. end,
  29. [end[0], start[1]],
  30. start,
  31. ],
  32. ])
  33. return geometry
  34. }
  35. maxPoints = 2
  36. }
  37. draw = new ol.interaction.Draw({
  38. source: source,
  39. type: type,
  40. geometryFunction: geometryFunction,
  41. maxPoints: maxPoints,
  42. })
  43. // callback && draw.on('drawend', callback)
  44. if (callback) {
  45. draw.on('drawend', callback)
  46. }
  47. return draw
  48. }

二、例子

2-1、拉框查询点

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <script src="./dist/include-openlayers-local.js"></script>
  8. <script src="./js/TIAN.js"></script>
  9. <script src="./js/Draw.js"></script>
  10. </head>
  11. <body>
  12. <button onclick="active()">激活</button>
  13. <button onclick="exit()">退出</button>
  14. <div id="map_container">
  15. </div>
  16. <script>
  17. var docLayer = new Zondy.Map.Doc('', 'smart_city', {
  18. ip: "localhost",
  19. port: 6163
  20. })
  21. var map = new ol.Map({
  22. target: "map_container",
  23. layers: [gaodeMapLayer, docLayer],
  24. view: new ol.View({
  25. projection: 'EPSG:4326',
  26. center: [114.30, 30.50],
  27. zoom: 4
  28. })
  29. })
  30. /* 1、创建画布 */
  31. var source = new ol.source.Vector({
  32. wrapX: false
  33. });
  34. var layer = new ol.layer.Vector({
  35. source
  36. })
  37. map.addLayer(layer);
  38. /* 2、激活画笔 */
  39. let draw = null;
  40. function active() {
  41. draw = createDraw({
  42. type: "Rectangle",
  43. source,
  44. callback: handleDraw
  45. })
  46. map.addInteraction(draw);
  47. }
  48. function handleDraw(e) {
  49. /* 3、实现查询服务 */
  50. //创建一个用于查询的区
  51. /* 3.1 创建查询的几何图形 */
  52. source.clear();
  53. const geomObj = new Zondy.Object.Polygon();
  54. //将ol的几何图形转换为中地的几何图形
  55. geomObj.setByOL(e.feature.getGeometry())
  56. /* 3.2 设置查询结构 */
  57. const queryStruct = new Zondy.Service.QueryFeatureStruct({
  58. IncludeGeometry: true,
  59. IncludeAttribute: true,
  60. IncludeWebGraphic: true
  61. });
  62. /* 3.3 设置查询的规则 */
  63. const rule = new Zondy.Service.QueryFeatureRule({
  64. //是否相交
  65. Intersect: true
  66. });
  67. //3.4 实例化查询参数对象
  68. const queryParam = new Zondy.Service.QueryParameter({
  69. geometry: geomObj,
  70. struct: queryStruct,
  71. rule: rule
  72. });
  73. //3.5 实例化地图文档查询服务对象
  74. const queryService = new Zondy.Service.QueryDocFeature(queryParam, "smart_city", 0, {
  75. ip: "localhost",
  76. port: "6163" //访问IGServer的端口号,.net版为6163,Java版为8089
  77. });
  78. //执行查询操作,querySuccess为查询回调函数
  79. queryService.query(querySuccess, queryError);
  80. }
  81. function querySuccess(result) {
  82. var format = new Zondy.Format.PolygonJSON()
  83. //将MapGIS要素JSON反序列化 ol.Feature类型数组
  84. var features = format.read(result)
  85. console.log(features)
  86. }
  87. function queryError(e) {
  88. console.log(e)
  89. }
  90. function exit(){
  91. map.removeInteraction(draw);
  92. draw = null;
  93. source.clear();
  94. }
  95. /*
  96. 3、交互式查询
  97. - 3.1 创建查询的几何图形
  98. - 3.2 设置查询的结构
  99. - 3.3 设置查询规则
  100. - 3.4 创建查询参数
  101. - 3.5 调用查询的接口
  102. */
  103. </script>
  104. </body>
  105. </html>

2-2、封装拉框查询 Query.js

通过几何图形查

  1. class Query {
  2. static queryByGeom({
  3. geometry,
  4. service,
  5. callback
  6. }) {
  7. var queryStruct = new Zondy.Service.QueryFeatureStruct();
  8. //是否包含几何图形信息
  9. queryStruct.IncludeGeometry = true;
  10. //是否包含属性信息
  11. queryStruct.IncludeAttribute = true;
  12. //是否包含图形显示参数
  13. queryStruct.IncludeWebGraphic = false;
  14. //创建一个用于查询的区
  15. var geomObj = new Zondy.Object.Polygon();
  16. //将ol转化为mapgis的查询对象
  17. geomObj.setByOL(geometry);
  18. //指定查询规则
  19. var rule = new Zondy.Service.QueryFeatureRule({
  20. //是否将要素的可见性计算在内
  21. EnableDisplayCondition: false,
  22. //是否完全包含
  23. MustInside: false,
  24. //是否仅比较要素的外包矩形
  25. CompareRectOnly: false,
  26. //是否相交
  27. Intersect: true
  28. });
  29. //实例化查询参数对象
  30. var queryParam = new Zondy.Service.QueryParameter({
  31. geometry: geomObj,
  32. resultFormat: "json",
  33. struct: queryStruct,
  34. rule: rule
  35. });
  36. //实例化地图文档查询服务对象
  37. var queryService = new Zondy.Service.QueryDocFeature(queryParam, service.name, service.layerId, {
  38. ip: "localhost",
  39. port: "6163" //访问IGServer的端口号,.net版为6163,Java版为8089
  40. });
  41. //执行查询操作,querySuccess为查询回调函数
  42. queryService.query(callback);
  43. }
  44. }

2-3、使用

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <script src="./dist/include-openlayers-local.js"></script>
  8. <script src="./libs/gaode.js"></script>
  9. <script src="./utils/Draw.js"></script>
  10. <script src="./libs/Query.js"></script>
  11. </head>
  12. <body>
  13. <button onclick="activeDraw()">拉框查询</button>
  14. <div id="map_container">
  15. </div>
  16. <script>
  17. var docLayer = new Zondy.Map.Doc('', 'xzd_city', {
  18. ip: 'localhost',
  19. port: 6163
  20. })
  21. var map = new ol.Map({
  22. target: "map_container",
  23. layers: [gaode],
  24. view: new ol.View({
  25. projection: 'EPSG:4326',
  26. center: [114.30, 30.50],
  27. zoom: 4
  28. })
  29. })
  30. map.addLayer(docLayer)
  31. var source = new ol.source.Vector({
  32. wrapX: false
  33. });
  34. var layer = new ol.layer.Vector({
  35. source
  36. })
  37. map.addLayer(layer);
  38. let draw = null;
  39. function activeDraw() {
  40. draw = createDraw({
  41. type: "Rectangle",
  42. source,
  43. callback: handleDraw
  44. })
  45. map.addInteraction(draw)
  46. }
  47. function handleDraw(e) {
  48. source.clear();
  49. var geometry = e.feature.getGeometry();
  50. Query.queryByGeom({
  51. geometry,
  52. service:{
  53. name:"xzd_city",
  54. layerId:2
  55. },
  56. callback:querySuccess
  57. })
  58. }
  59. //查询成功回调
  60. function querySuccess(result) {
  61. var format = new Zondy.Format.PolygonJSON();
  62. //将MapGIS要素JSON反序列化为ol.Feature类型数组
  63. var features = format.read(result);
  64. console.log(features)
  65. }
  66. </script>
  67. </body>
  68. </html>