zhongrj
2023-06-27 d74b495736f0c04522ec54274b70fed0ded2878d
public/map/core/manager/routemanager.js
New file
@@ -0,0 +1,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;
      }
   });
});