GIS开发之二维地下管线综合管理系统(Arcgis)第四节 查询(1)

前言

ArcGIS Api for JavaScript 查询接口分为属性查询和空间查询两种,具体使用的类如下:

  • 属性查询:QueryTask,FindTask(只能属性,多个图层)
  • 空间查询:IdentifyTask(多个图层),QueryTask(单个图层)

本文主要介绍空间查询函数的使用方法及函数封装过程

IdentifyTask(点击查询)

功能介绍:鼠标与地图交互实现,通过点击地图上某个图层的点、线、面信息,查询鼠标点击位置要素详情。通过解析返回数据格式,进行页面展示

  1. 类引入
//定义全局变量,点击查询任务和查询参数
var identifyTask, identifyParams;
var callbackFun;
var defaultParam;
define(["esri/tasks/IdentifyParameters", "esri/tasks/IdentifyTask"], function (IdentifyParameters, IdentifyTask){
	// 实现代码
	function QYIdentifyTask() {
		......
		return QYIdentifyTask;
	}
}
  1. 定义默认参数
this.defaultParams = {
	// 点击查询的图层地址
	url: "http://localhost:5013/arcgis/rest/services/test/MapServer",
	// 容差
	tolerance: 3,
	// 是否返回地理要素信息
    returnGeometry: true,
    // url中的图层号
   	layerIds: [2]
};
defaultParam = this.defaultParams;
  1. 参数设置
this.doIdentifyTask = function (options, callback) {
    var that = this;
    // 设置回调
    callbackFun = callback;
    identifyTask = new IdentifyTask(options.url || defaultParam.url);
    identifyParams = new IdentifyParameters();
    identifyParams.tolerance = options.tolerance || defaultParam.tolerance;
    identifyParams.returnGeometry = options.returnGeometry || defaultParam.returnGeometry; //是否返回集合对象
    identifyParams.layerIds = options.layerIds || defaultParam.layerIds;
    //        identifyParams.layerOption = IdentifyParameters.LAYER_OPTION_ALL; //指定使用哪个层在使用识别
    identifyParams.width = options.width || MWP.map.width; //当前正在查看地图的像素高度
    identifyParams.height = options.height || MWP.map.height; //当前正在查看地图的像素宽度
    identifyParams.mapExtent = options.extent || MWP.map.extent; //地图范围
    if (options.geometry) {
        identifyParams.geometry = options.geometry;
        that.execute(); // 执行点击查询函数
    }
};
  1. 执行查询
this.execute = function () {
    var that = this;
     identifyTask.execute(identifyParams,
      function (evt) {
          callbackFun.success(evt);
      },
      function (err) {
          callbackFun.fail(err);
      });
}
  1. 完整代码如下
//定义全局变量,点击查询任务和查询参数
var identifyTask, identifyParams;
var callbackFun;
var defaultParam;

define(["esri/tasks/IdentifyParameters", "esri/tasks/IdentifyTask"], function (IdentifyParameters, IdentifyTask) {

    // 清淤检测点击查询构造函数
    function QYIdentifyTask() {
        this.defaultParams = {
            url: "http://localhost:5013/arcgis/rest/services/test/MapServer",
            tolerance: 3,
            returnGeometry: true,
            layerIds: [2]
        };

        defaultParam = this.defaultParams;

        this.doIdentifyTask = function (options, callback) {
            var that = this;
            callbackFun = callback;
            identifyTask = new IdentifyTask(options.url || defaultParam.url);
            identifyParams = new IdentifyParameters();
            identifyParams.tolerance = options.tolerance || defaultParam.tolerance;
            identifyParams.returnGeometry = options.returnGeometry || defaultParam.returnGeometry; //是否返回集合对象
            identifyParams.layerIds = options.layerIds || defaultParam.layerIds;
            identifyParams.width = options.width || MWP.map.width; //当前正在查看地图的像素高度
            identifyParams.height = options.height || MWP.map.height; //当前正在查看地图的像素宽度
            identifyParams.mapExtent = options.extent || MWP.map.extent; //地图范围
            if (options.geometry) {
                identifyParams.geometry = options.geometry;
                that.execute();
            }
        };

        this.execute = function () {
            var that = this;
            identifyTask.execute(identifyParams,
          function (evt) {
              callbackFun.success(evt);
          },
          function (err) {
              callbackFun.fail(err);
          });
        }
    }
    return QYIdentifyTask;
});
  1. 调用
MWP.QYJC.click2QueryEvent = On.once(MWP.map, "click", function (me) {
    var QYClickTask = new QYIdentifyTask();
    var options = {
        geometry: me.mapPoint
    }
    // 此处options中设置的各项参数在执行是可覆盖封装函数中的对应的默认参数,如未设置,则选用默认参数
    QYClickTask.doIdentifyTask(options, {
        success: function (result) {
            if (result.length > 0) {
                // 后续处理...
            } else {
                MWP.showMapWarning("无查询结果!", "提示");
            }
            //返回数据成功
            console.log("res:->");
            console.log(result);
        },
        fail: function (err) {
            //查询失败
            MWP.showMapWarning("查询错误!\n" + err, "提示");
            MWP.map.setMapCursor("default");
            console.log("err:->");
            console.log(err);
        }
    });
})

参考资料

  1. arcgis api官方教程

下一篇主要分享QueryTask类的分析与函数封装,用以实现空间范围和属性结合查询功能

关注以下公众号,及时发布各种技术交流,并下载相关文档和程序

Alt

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值