前言
ArcGIS Api for JavaScript 查询接口分为属性查询和空间查询两种,具体使用的类如下:
- 属性查询:QueryTask,FindTask(只能属性,多个图层)
- 空间查询:IdentifyTask(多个图层),QueryTask(单个图层)
本文主要介绍空间查询函数的使用方法及函数封装过程
IdentifyTask(点击查询)
功能介绍:鼠标与地图交互实现,通过点击地图上某个图层的点、线、面信息,查询鼠标点击位置要素详情。通过解析返回数据格式,进行页面展示
- 类引入
//定义全局变量,点击查询任务和查询参数
var identifyTask, identifyParams;
var callbackFun;
var defaultParam;
define(["esri/tasks/IdentifyParameters", "esri/tasks/IdentifyTask"], function (IdentifyParameters, IdentifyTask){
// 实现代码
function QYIdentifyTask() {
......
return QYIdentifyTask;
}
}
- 定义默认参数
this.defaultParams = {
// 点击查询的图层地址
url: "http://localhost:5013/arcgis/rest/services/test/MapServer",
// 容差
tolerance: 3,
// 是否返回地理要素信息
returnGeometry: true,
// url中的图层号
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.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(); // 执行点击查询函数
}
};
- 执行查询
this.execute = function () {
var that = this;
identifyTask.execute(identifyParams,
function (evt) {
callbackFun.success(evt);
},
function (err) {
callbackFun.fail(err);
});
}
- 完整代码如下
//定义全局变量,点击查询任务和查询参数
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;
});
- 调用
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);
}
});
})
参考资料
下一篇主要分享QueryTask类的分析与函数封装,用以实现空间范围和属性结合查询功能
关注以下公众号,及时发布各种技术交流,并下载相关文档和程序