您的位置:首页 > Web前端 > JavaScript

ArcGIS JavaScript API使用Promise进行公交换乘查询及结果绘制

2017-09-01 09:34 507 查看
两个方法,一个用于根据站点名称获得站点的点对象(包含坐标信息),另一个是根据站点信息进行线路的处理及展示。代码如下:

/**
* 获取公交站点
* @param {[type]} stationName [站点名称]
* @param {[type]} lineName [站点所在线路名称]
* @return {[type]} [Point,包含坐标]
*/
getStationPoint:function(stationName,lineName){
var p = new Promise(function(resolve,reject){
var queryTask = new QueryTask(this.busStationLayerURLProperty);
var query = new Query();
query.returnGeometry=true;
var where=this.belongLineNameProperty+"='"+lineName+"' and "+this.stationNameProperty+"= '"+stationName+"'";
query.where = where;
query.outFields=["*"];
queryTask.execute(query).then(function(result){
if(1!=result.features.length){
reject("bad result");
}else {
resolve(result.features[0].geometry);
}
}.bind(this));
}.bind(this));
return p;
},

/**
* 获取及显示换乘线路
* @param {[type]} lineNames [线路名,如有多条,以逗号隔开]
* @return {[type]} [description]
*/
getChangeLine: function(lineNames) {
_this = this;
var queryTask = new QueryTask(_this.busLineLayerURLProper
4000
ty);
var query = new Query();
query.returnGeometry = true;
var where = "";
for (var i = 0; i < lineNames.length; i++) {
if (i == 0) {
where = _this.lineNameProperty + "= '" + lineNames[i] + "'";
} else
where = where + " or " + _this.lineNameProperty + "= '" + lineNames[i] + "'";
}

//获取出发站、到达站、换乘站
var startStation = dom.byId(_this.startStationId).getElementsByTagName("input")[0].value;
var endStation = dom.byId(_this.endStationId).getElementsByTagName("input")[0].value;
var changeStation = _this.currentChangeStation;

query.where = where;
query.outFields = ["*"];
queryTask.execute(query, function(lines) {
var numLine = lines.features.length;
if (numLine > 1) {
var startLine; //第一条线路Polyline的乘车区间段
var endLine; //第二条线路Polyline的乘车区间段
var firstLine; //第一条线路Polyline对象
var secondLine; //第二条线路Polyline对象
var lineName1 = lineNames[0]; //第一条线路名
var lineName2 = lineNames[1]; //第二条线路名

for (var i = 0; i < 2; i++) {
if (lines.features[i].attributes[_this.lineNameProperty] === lineName1) {
firstLine=lines.features[i].geometry;
Promise //根据站点名获取站点(Point),两个站点都获取完成后,进行乘车区间切割
.all([_this.getStationPoint(changeStation,lineName1),_this.getStationPoint(startStation,lineName1)])
.then(
function(stations){
console.log("resolved start");
if(2==stations.length){
startLine = _this.getCutLine(stations[0], stations[1], firstLine); //切割乘车区间
if (startLine) { //绘制乘车区间
var graphicS = new Graphic(startLine);
graphicS.setSymbol(_this.simpleLineSymbol);
_this.graphicLayer.add(graphicS);
}
}
}
);
} else {
secondLine= lines.features[i].geometry;
Promise
.all([_this.getStationPoint(changeStation,lineName2),_this.getStationPoint(endStation,lineName2)])
.then(
function(stations){
console.log("resolved end");
endLine = _this.getCutLine(stations[0], stations[1], secondLine);
if (endLine) {
var graphicE = new Graphic(endLine);
graphicE.setSymbol(_this.simpleLineSymbol);
_this.graphicLayer.add(graphicE);
}
}
);
}
}

//合并线路,获得外接矩形范围并显示
var unionGeometry = geometryEngine.union(lines.features[0].geometry,lines.features[1].geometry);
var newExtent = unionGeometry.getExtent();
_this.map.setExtent(newExtent.expand(2));
console.log("OK");
} else {
error("查找的换乘线路少于两条,无法获取线路!");
return false;
}
});
},
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐