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

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

        /**
         * 获取公交站点
         * @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.busLineLayerURLProperty);
            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;
                }
            });
        },


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值