Select to view content in your preferred language

Combining a drivetime and identify widget.

2547
12
08-01-2011 03:00 PM
SeanCook
Emerging Contributor
Hello everyone. I recently began playing with flex and I am trying to accomplish a task I think should be straight forward but am having some difficulty with. I am trying to create a widget which calculates a drive time, and returns an identify on the objects in the returned drive time.

I can add a button to the identify widget, which when click looks for a map click and calculates a drive time. Now, when a drive time is calculated using the first shown function, is the
variable FeatureSet a geometry variable that I can feed into the second identify features function?

Will that be enough to get this tool working? Any other advice on how it could or should be done?

Sorry about the vague question, but I am getting going and having some trouble with this one.


[PHP]
   private function computeServiceArea(mapPoint:MapPoint):void
   {
   
    myGraphicsLayer.clear();
    var graphic:Graphic = new Graphic(mapPoint, sms_circleAlphaSizeOutline);
    myGraphicsLayer.add(graphic);
    var featureSet:FeatureSet = new FeatureSet([ graphic ]);
    var params:Object =
     {
      "Input_Location": featureSet,
      "Drive_Times": driveTimes
     };
    gp.execute(params, new AsyncResponder(onResult, onFault));
    function onResult(gpResult:ExecuteResult,token:Object = null):void
    {
     var pv:ParameterValue = gpResult.results[0];
     var fs:FeatureSet = pv.value as FeatureSet;
     myGraphicsLayer.graphicProvider = fs.features;
     myGraphicsLayer.renderer=uniqueValueRenderer;
    }
    function onFault(info:Object, token:Object = null):void
    {
     Alert.show(info.toString());
    }
   }
  
[/PHP]

[PHP]
  
  
  
   //identify features
   private function identifyFeatures(geom:Geometry):void
   {
    var identifyParams:IdentifyParameters = new IdentifyParameters();
    identifyParams.returnGeometry = returnGeomForZoom;
    identifyParams.tolerance = identifyTolerance;
    identifyParams.geometry = geom;
    identifyParams.width = map.width;
    identifyParams.height = map.height;
    identifyParams.mapExtent = map.extent;
    identifyParams.spatialReference = map.spatialReference;
    if(betaReturnGeometryFix){
     var geomString:String = JSON.encode(geom).replace(',"spatialReference":{"wkid":' + geom.spatialReference.wkid + '}','');
     var idURL:String = "?geometryType=" + geom.type + "&geometry=" + geomString;
     idURL += "&sr=" + map.spatialReference.wkid.toString() + "&layers="
     var idURL2:String = "&tolerance=" + identifyTolerance.toString() + "&mapExtent=" + map.extent.xmin.toString() + "," + map.extent.ymin.toString() + "," +  map.extent.xmax.toString() + "," +  map.extent.ymax.toString();
     idURL2 += "&imageDisplay=" + map.width.toString() + "," + map.height.toString() + ",96" + "&returnGeometry=" + returnGeomForZoom.valueOf().toString() + "&f=json";
    }
    for (var i:Number = map.layerIds.length -1; i >= 0; i--)
    {
     identifyParams.layerOption = identifyLayerOption;
     var layer:Layer = map.getLayer(map.layerIds);
     identifyParams.layerIds = null;
     var url:String = "";
    
     //HAB:Does this layer require a proxy???
     var useProxy:Boolean = false;
    
    
     //HAB:loop thru proxy layer list and determine which layer needs a proxy
     for(var p:int=0; p<configIdentProxyLayers.length; p++)
     {
      //HAB:set the proxy flag if needed
      if (layer.name == configIdentProxyLayers

.name)


      {
       useProxy = true;
      }
     }
    
     if (layer is ArcGISDynamicMapServiceLayer)
     {
      var dynamicLayer:ArcGISDynamicMapServiceLayer = layer as ArcGISDynamicMapServiceLayer;
      url = dynamicLayer.url;
      if(identifyLayerOption == "visible")
      {
       if(dynamicLayer.visible == false){
        url="";
       }else{
        if(dynamicLayer.visibleLayers){
         identifyParams.layerIds = new Array();
         for (var j:Number = 0; j < dynamicLayer.visibleLayers.source.length; j++)
         {
          if (dynamicLayer.layerInfos[dynamicLayer.visibleLayers.source] &&
           !dynamicLayer.layerInfos[dynamicLayer.visibleLayers.source].subLayerIds)
          {
           identifyParams.layerIds.push(dynamicLayer.visibleLayers.source);
          }
         }
         identifyParams.layerOption = "all";
        }
       }
      }
     }
     else if (layer is ArcGISTiledMapServiceLayer)
     {
      var tiledLayer:ArcGISTiledMapServiceLayer = layer as ArcGISTiledMapServiceLayer;
      url = tiledLayer.url;
      if(identifyLayerOption == "visible")
      {
       if(tiledLayer.visible == false)
        url="";
      }
     }
     else if (layer is FeatureLayer)
     {
      var featLayer:FeatureLayer = layer as FeatureLayer;
      url = featLayer.url;
      var layId:int = -1;
      var arcDL:ArcGISDynamicMapServiceLayer;
     
      if( url.indexOf("FeatureServer") > -1)
      {
       var msName:String = url.replace("FeatureServer","MapServer");
       arcDL = new ArcGISDynamicMapServiceLayer(msName.substring(0,msName.lastIndexOf("/")));
       url = arcDL.url;
       layId = parseInt(msName.substring(msName.lastIndexOf("/")+ 1));
      }else{
       arcDL = new ArcGISDynamicMapServiceLayer(url.substring(0,url.lastIndexOf("/")));
       layId = parseInt(url.substring(url.lastIndexOf("/")+ 1));
       url = arcDL.url;
      }
     
      if(identifyLayerOption == "visible")
      {
       if(layId != -1)
        identifyParams.layerIds = [layId];
       if(featLayer.visible == false)
        url="";
      }
     }
    
     if(url)
     {
      if(betaReturnGeometryFix){
       var visString:String = "";
       if(identifyParams.layerIds){
        visString += ":";
        for(var v:int=0; v<identifyParams.layerIds.length; v++)
        {
         if(v>0)
          visString += ","
         visString += identifyParams.layerIds.toString();
        }
       }
       var cIdURL:String = url + "/identify" + idURL + identifyLayerOption + visString + idURL2;
       var iService:HTTPService = new HTTPService();
       iService.url = cIdURL;
       iService.resultFormat = "text";
       iService.addEventListener(ResultEvent.RESULT, buildIdResults);
       iService.addEventListener(FaultEvent.FAULT, onFault);
       iService.send();
      }else{
       var identifyTask:IdentifyTask = new IdentifyTask(url);
       //HAB:apply proxy if needed
       if(useProxy)identifyTask.proxyURL = proxyURL;
       identifyTask.addEventListener(IdentifyEvent.EXECUTE_COMPLETE, onResult);
       identifyTask.addEventListener(FaultEvent.FAULT, onFault);
       identifyTask.execute(identifyParams);
      }      
      showMessage(loadingLabel, true);
      showStateResults();  
     }
    }
   }  
  
   private function buildIdResults(event:ResultEvent):void
   {
    var rawData:String = String(event.result);
    var data:Object = JSON.decode(rawData);
    var retArray:Array = [];
    if(data.error){
     //do nothing;
    }else{
     for each( var idObj:Object in data.results)
     {
      var idResult:IdentifyResult = new IdentifyResult();
      idResult.displayFieldName = idObj.displayFieldName;
      idResult.layerId = idObj.layerId;
      idResult.layerName = idObj.layerName;
      idResult.feature = buildGra(idObj);
      retArray.push(idResult);
     }
    }
    function buildGra(obj:Object):Graphic
    {
     var gra:Graphic;
     switch(obj.geometryType)
     {
      case Geometry.MAPPOINT:
      {
       var mp:MapPoint = new MapPoint(obj.geometry.x, obj.geometry.y, new SpatialReference(obj.geometry.spatialReference.wkid));
       gra = new Graphic(mp);
       gra.attributes = obj.attributes;
       break;
      }
      case Geometry.POLYGON:
      {
       var mPoly:Polygon = new Polygon(null);
       for (var i2:int = obj.geometry.rings.length - 1; i2 >= 0; i2--)
       {
        var ringArray:Array = [];
        for (var j1:int = 0; j1 < obj.geometry.rings[i2].length; j1++)
        {
         var mpStr:String = obj.geometry.rings[i2][j1];
         var xyArr:Array = mpStr.split(",")
         var mp2:MapPoint = new MapPoint(xyArr[0], xyArr[1]);
         mp2.spatialReference = new SpatialReference(obj.geometry.spatialReference.wkid);
         ringArray.push(mp2);
        }
        mPoly.addRing(ringArray);
       }
       mPoly.spatialReference = new SpatialReference(obj.geometry.spatialReference.wkid);
      
       gra = new Graphic(mPoly);
       gra.attributes = obj.attributes;
       break;
      }
      case Geometry.EXTENT:
      {
       var ext:Extent = new Extent(obj.geometry.xmin, obj.geometry.ymin, obj.geometry.xmax, obj.geometry.ymax, new SpatialReference(obj.geometry.spatialReference.wkid));
       gra = new Graphic(ext);
       gra.attributes = obj.attributes;
       break;
      }
      case Geometry.POLYLINE:
      {
       var pLine:Polyline = new Polyline(obj.Geometry.paths, new SpatialReference(obj.geometry.spatialReference.wkid));
       gra = new Graphic(pLine);
       gra.attributes = obj.attributes;
       break;
      }
     }
     return gra;
    }
    onResult(new IdentifyEvent("",retArray));
   }
   [/PHP]
Tags (2)
0 Kudos
12 Replies
RobertScheitlin__GISP
MVP Emeritus
Sean,

   After making the addition of the drive time to the identify widget myself It is immediately apparent that the identify task is incapable to using such complicated geometry for it's input. I have gone as far as extracting just the exterior most ring of the polygon and yet it still fails 95% of the time I have also tried to ensure the polygon is simple by sending through the simplify routine on the geometry service to no avail. I wound now say that any attempt to use this two together are doomed for disappointment.

BTW: My plate is overloaded as it is so I don't look for anything else to fall off it.
0 Kudos
SeanCook
Emerging Contributor
Sean,

   After making the addition of the drive time to the identify widget myself It is immediately apparent that the identify task is incapable to using such complicated geometry for it's input. I have gone as far as extracting just the exterior most ring of the polygon and yet it still fails 95% of the time I have also tried to ensure the polygon is simple by sending through the simplify routine on the geometry service to no avail. I wound now say that any attempt to use this two together are doomed for disappointment.

BTW: My plate is overloaded as it is so I don't look for anything else to fall off it.



Fair enough, and thanks for all the help! Would that mean, if I want to make these results available, using the geoprocessor to send the request back to the server?

I still have been unable to get the drivetime task to execute and that is bothering me quite a bit, I'd really like to understand why that would be the case. I am beginning to think there is something wrong with my server or flash builder, although it seems unlikely. The above post wasn't entirely clear and part of the quoted code got chopped, but those two strings were in response to a

trace (featureSet);


and the results either change to the shorter non-functional parameters for the drivetime gp call in my drivetime widget after being opened and closed once, or in the case of my identify code, never returns a proper graphic. Somehow this line:

var graphic:Graphic = new Graphic(mapPoint, sms_circleAlphaSizeOutline);


seems to be giving me to different outputs for the same input.
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Sean,

   In the init function of the Identify widget did you ever add the uniquevaluerenderer?

graphicsLayer = new GraphicsLayer();
                graphicsLayer.symbol = identPicSymbol;
                graphicsLayer.renderer = uniqueValueRenderer;
                map.addLayer(graphicsLayer);


This is what is needed for the drive time results to be displayed on the map.
0 Kudos