zengh
2022-05-16 63ad2c3598400370dd7da5534659fd7e768a0a4a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
 * 路线搜索
 * @author Liuyl
 * @date 2015/11/1p
 */
define([
"dojo/_base/declare", "dojo/_base/array", "dojo/_base/lang",
"esri/tasks/RouteTask", "esri/tasks/RouteParameters", "esri/tasks/FeatureSet",
"base/AppEvent", "base/ConfigData", "esri/graphic","base/bustransfer/TransferParameter", 
"base/bustransfer/TransferTask"
], function(
    declare, arrayUtil, lang,
    RouteTask, RouteParameters, FeatureSet,
    AppEvent, ConfigData, Graphic,TransferParameter,
    TransferTask
) {
    return declare("RouteManager", null, {
        QueryArguments: declare(null, {
            constructor: function(props) {
                this.id = uuid();
                this.queryType = null;
                this.reqId = null;
                this.extent = null;
                this.poiType = null;
                this.keyword = null;
                this.queryForCount = false;
                this.returnGeometry = true;
                this.start = 0;
                this.num = 10;
                this.outSpatialReference = null;
                this.district = null;
                $.extend(true, this, props);
            }
        }),
        defaultOptions: {
            route_service: "http://www.servicelnditu.com/OneMapServer/rest/services/route2014/NAServer/Route",
            bus_service:"http://www.servicelnditu.com/OneMapServer/rest/services/ShenYangBus/Transfer/exts/PublicTransportationSOE",
            result_widget_name: "RouteResultWidget"
        },
        constructor: function(options) {
            this.options = {};
            $.extend(true, this.options, this.defaultOptions, options);
            this.resultWidgetIsOpen = false;
 
            AppEvent.addAppEventListener(AppEvent.CONFIG_LOADED, lang.hitch(this, this.onConfigLoaded));
            AppEvent.addAppEventListener(AppEvent.BASE_MAP_LAYER_LOADED, lang.hitch(this, this.onMapLoaded));
            AppEvent.addAppEventListener(AppEvent.RUN_ROUTE_QUERY, lang.hitch(this, function(evt) {
                this.query(evt);
            }));
 
            this.newestQueryId = null;
        },
        onConfigLoaded: function() {},
        onMapLoaded: function(map) {
            this.map = map;
        },
 
 
        query: function(evt) {
            try {
                var queryArgs = new this.QueryArguments();
                $.extend(true, queryArgs, evt);
                this.newestQueryId = queryArgs.id;
                this._do_query(queryArgs);
            } catch (err) {
                this._errorCallback(err, {
                    hockId: evt.hockId
                });
            }
        },
        _do_query: function(queryArgs) {
            if (queryArgs.type == "bus") {
                this.queryBusRoute(queryArgs,
                    lang.hitch(this, this._resultCallback),
                    lang.hitch(this, this._errorCallback)
                );
            } else if (queryArgs.type == "car") {
                this.queryCarRoute(queryArgs,
                    lang.hitch(this, this._resultCallback),
                    lang.hitch(this, this._errorCallback)
                );
            }
        },
 
        _resultCallback: function(result, qArgs) {
            if (this.newestQueryId == qArgs.id) {
                AppEvent.dispatchAppEvent(AppEvent.QUERY_SUCCESS, {
                    hockId: qArgs.hockId,
                    result: result
                });
            }
 
        },
        _errorCallback: function(err, qArgs) {
            if (this.newestQueryId == qArgs.id) {
                AppEvent.dispatchAppEvent(AppEvent.QUERY_ERROR, {
                    hockId: qArgs.hockId,
                    error: err
                });
            }
        },
        queryBusRoute: function(qArgs, resultCallback, errorCallback) {
            var pac = "";
            if(ConfigData["districts"] == "" || ConfigData["districts"] == undefined){
                errorCallback({message:"公交换乘服务未加载完成,请重试"}, qArgs);
                return;
            }
            for(var i = 1; i < ConfigData["districts"].length; i++){
                if(ConfigData["districts"][i]["Polygon"].contains(qArgs.start.geometry)){
                    pac = ConfigData["districts"][i]["PAC"];
                    break;
                }
            }
            if(pac == "2100" || pac == ""){
                errorCallback({message:"请求范围已超出辽宁省"}, qArgs);
                return;
            }
            var thisObj = this;
            var prior = "time";
            if(!(qArgs.searchtype == "" || qArgs.searchtype == undefined)){
                prior = qArgs.searchtype;
            }
            var transferParameter = new TransferParameter();
            transferParameter.departure = "{\"x\":" + qArgs.start.geometry.x + ",\"y\":" + qArgs.start.geometry.y + "}";
            transferParameter.destination = "{\"x\":" + qArgs.end.geometry.x + ",\"y\":" + qArgs.end.geometry.y + "}";
            transferParameter.prior = prior;
            transferParameter.exclusive = "mixed";
            transferParameter.inSR = this.map.spatialReference;
            var transferTask = new TransferTask(ConfigData["busServices"][pac] + "/exts/PublicTransportationSOE/transfer");
            transferTask.execute(transferParameter,function(results){
                $.extend(true, results, {"start":qArgs.start.geometry,"end":qArgs.end.geometry});
                results.searchtype=prior;
                results.routeType="bus";
                results.pac = pac;
                resultCallback(results, qArgs);
            },function(err){
                errorCallback(err.error, qArgs);
            });
        },
        queryCarRoute: function(qArgs, resultCallback, errorCallback) {
            var impedanceAttribute="Time";
            if(qArgs.searchtype){
                switch(qArgs.searchtype){
                case "time":
                    impedanceAttribute="Minutes";
                    break;
                case "distance":
                    impedanceAttribute="Meters";
                    break;
                default:
                    break;
                }
            }
            var routeTask = new RouteTask(this.options.route_service);
            var routeParas = new RouteParameters();
            routeParas.barriers = new FeatureSet();
            routeParas.stops = new FeatureSet();
            routeParas.returnDirections = true;
            routeParas.returnRoutes = true;
            routeParas.outSpatialReference = this.map.spatialReference;
            routeParas.impedanceAttribute=impedanceAttribute;
            var g1 = this.filter(qArgs.start);
            var g2 = this.filter(qArgs.end);
 
            routeParas.stops.features.push(g1);
            routeParas.stops.features.push(g2);
 
            routeTask.solve(routeParas, lang.hitch(this, function(result) {
                result.searchtype=qArgs.searchtype;
                result.routeType="car";
                resultCallback(result, qArgs);
            }), lang.hitch(this, function(err) {
                errorCallback({message:"未能查询到路线结果!"}, qArgs);
            }));
        },
        filter: function(graphic) {
            var g = new Graphic(graphic.geometry);
            g.setAttributes({
                NAME: graphic.attributes.NAME
            })
            return g;
        }
    });
});