AnsweredAssumed Answered

identify dynamicservice layers and edit feature layer

Question asked by bchepuri on Feb 11, 2014
Latest reply on Feb 27, 2015 by kmsagis
I am trying to combine two scripts that I found on esri.com into one script -
1. identify two mapservice layers - "click" event identify task and popups - works well.
2. edit a featureservice layer using - "layers-add-result" event and template picker - doesn't work.

I would like to have the application to do the following -
1. when  clicked on a feature from mapservice (polygons) the identify poppup window should display results
2. but if clicked on  point feature service only the edit functionality should work.  points and polygons do overlap.

I am just getting my feet wet in esri javascript api.  Any help is really appreciated.

Thank you!

BC




var precURL = "http://server/arcgis/rest/services/Polys/MapServer"

var addressURL = "http://server/arcgis/rest/services/Points/FeatureServer/0";

var visibleLayers = [0,1];

var mapTitle = "Address pts";

var map, mapLayer, featureLayer, fLayer;

/*
Define the map application script using "require" to load the necessary modules. 
See documentation here: "http://dojotoolkit.org/documentation/tutorials/1.8/modules/" for more information.
This includes standard ESRI modules as well as custom modules included with the application.
*/


require([
 "esri/map", 
 "esri/dijit/HomeButton",
 "esri/geometry/Extent", 
 "esri/layers/ArcGISDynamicMapServiceLayer",  
 "esri/layers/FeatureLayer", 
 "esri/dijit/Scalebar", 
 "esri/dijit/PopupTemplate", 
 "esri/dijit/Popup", 
 "esri/dijit/Geocoder",
 
 "esri/tasks/IdentifyTask", 
 "esri/tasks/IdentifyParameters", 
 "esri/tasks/IdentifyResult", 
 "esri/geometry/screenUtils",
 
 "dojo/_base/array", 
 "dojo/parser", 
 "modules/customBasemaps", 
 "modules/clickLegend",
 "dojo/dom", 
 "dojo/dom-construct",
 "esri/config",
   
 "esri/toolbars/edit",
 "esri/layers/ArcGISTiledMapServiceLayer",
 "esri/symbols/SimpleMarkerSymbol",
 "esri/dijit/editing/Editor-all",
 "esri/dijit/editing/TemplatePicker",
 "esri/config",
 "dojo/i18n!esri/nls/jsapi",
 "dojo/keys",
 
  "dijit/layout/BorderContainer", "dijit/layout/ContentPane", 
  "dojo/domReady!"

 ],
 function(
  Map, 
  HomeButton,
  Extent, 
  ArcGISDynamicMapServiceLayer, 
  FeatureLayer,
  Scalebar, 
  PopupTemplate, 
  Popup, 
  Geocoder,
    
  IdentifyTask, 
  IdentifyParameters, 
  IdentifyResult, 
  screenUtils,
    
  arrayUtils, 
  parser, 
  CustomBasemaps, 
  ClickLegend, 
  dom, 
  domConstruct,
  esriConfig,

  Edit, 
  ArcGISTiledMapServiceLayer, 

  SimpleMarkerSymbol, 
  //SimpleLineSymbol, 
    
  Editor, 
  TemplatePicker,
    
   
  jsapiBundle, 
  keys
 ) {
  
  parser.parse();
  
  // Set the document (webpage) title.
  document.title = mapTitle;

  // refer to "Using the Proxy Page" for more information:  https://developers.arcgis.com/en/javascript/jshelp/ags_proxy.html
        esriConfig.defaults.io.proxyUrl = "/proxy"; 
  

       
  // Create a dynamic map service layer object using the global mapLayer variable.
  mapLayer = new ArcGISDynamicMapServiceLayer(precURL);

  // Address layer
  featureLayer = new FeatureLayer(addressURL,{
    mode: FeatureLayer.MODE_ONDEMAND, 
    outFields: ['*']
   });
  
  //labels
  var labels = new ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/Reference/World_Boundaries_and_Places/MapServer");
  
  //change in prod
  esriConfig.defaults.geometryService = new esri.tasks.GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");

 
  // Set a variable to the extent with the desired spatial reference.

  
  // Create the map variable with basemap, extent, and minScale.
  map = new esri.Map("map", {
          basemap: "streets",
          center: [-70.34, 30.0],
          zoom: 10
  });
    
  //Legend   
    var legend = new ClickLegend();
    // Send the service url to the "setLayers" method of ClickLegend to create the legend.
       legend.setLayers([precURL]);
    
  // Use the custom basemap module to create create a basemap gallery with the desired basemap layers.
  // The layers can be changed by editing the code in the "customBasemaps.js" module.    
  var basemapGallery = new CustomBasemaps({
          showArcGISBasemaps: false,
          map: map
        }, "basemapMenu");
        basemapGallery.startup();
  
  
        // refer to "Using the Proxy Page" for more information:  https://developers.arcgis.com/en/javascript/jshelp/ags_proxy.html
        //esriConfig.defaults.io.proxyUrl = "/proxy";    
    
  // Set functions to occur once the map has loaded.
  map.on("load", function(){
   // Using ESRI module, add a scale bar to the map. CSS style can be modified in layout.css.
   var scalebar = new Scalebar({
    map:map,
    scalebarUnit:"english"
   });
   
   //Home button
   var home = new HomeButton({
    map: map
    }, "HomeButton");
    home.startup();
   
   // Using ESRI module, add a new geocoder search widget. CSS style can be modified in layout.css.
   var search = new Geocoder({
    map:map
   }, "search");
   search.startup();
   
   
   // Set legal disclaimer language.
           //alert("This GIS data product to be used for reference purposes only and is not to be construed as a legal document.");
   
   var layerDefinitions = [];
   layerDefinitions[0] = "COUNTY_ID = 1";
   layerDefinitions[1] = "COUNTY_ID = 1";
   mapLayer.setLayerDefinitions(layerDefinitions);
   // Add the layer to the map.
   map.addLayers([mapLayer,labels,featureLayer]);
  
   // Set the visible layers using the visibleLayers variable.
   mapLayer.setVisibleLayers(visibleLayers);
    
  });
    
  // Set identifyTask to run when the map is clicked.
  map.on("click", function(evt){
  
  // This code creates the identify functionality when a feature on the map is clicked.
  //First create the identifyTask and identifyParameters variables. Then set the identifyParameters properties.
  
   var identifyTask = new IdentifyTask(precURL);
   var identifyParams = new IdentifyParameters();
   identifyParams.tolerance = 3;
   identifyParams.returnGeometry = true;
   identifyParams.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_VISIBLE;
   identifyParams.width  = map.width;
   identifyParams.height = map.height;

   // Create a variable to hold the clicked point on the map.
   var clickPnt = evt.mapPoint;
         identifyParams.geometry = clickPnt;
         identifyParams.mapExtent = map.extent;
         identifyParams.layerIds = mapLayer.visibleLayers;
   //identifyParams.layerIds = [0,1];
   
   // Execute the identifyTask, which will return an array of popup templates to be stored in the "deferred" variable. 
         var deferred = identifyTask.execute(identifyParams);

         deferred.addCallback(function(response) {      
          // The idenitifyTask returns an array of identify result objects    
            // This callback function is used to change it to an array of features each with
            // a popup template based on the attribute fields for each feature.
            return dojo.map(response, function(result) {
              var feature = result.feature;
              var fieldInfo = [];
              for (var key in feature.attributes){
               if (feature.attributes.hasOwnProperty(key)){
                var info = {fieldName:"", visible:true};
                info.fieldName = key;
                fieldInfo.push(info);
               }
              };
              feature.setInfoTemplate(new PopupTemplate({
      title: result.layerName,
      fieldInfos: fieldInfo
     }));
              return feature;
            });
         });
      
         // InfoWindow expects an array of features from each deferred
         // object that you pass. If the response from the task execution 
         // above is not an array of features, then you need to add a callback
         // like the one above to post-process the response and return an
         // array of features.
         // Set the infoWindow features to the array stored in the "deferred" variable.
         map.infoWindow.setFeatures([ deferred ]);
         // Set the anchor point for the infoWindow to the clicked point.
         map.infoWindow.show(clickPnt); 
        });
     
        
   
  //editor  
  map.on("layers-add-result", initEditor); 
  
        function initEditor(evt) {
          var templateLayers = arrayUtils.map(evt.layers, function(result){
   return result.layer;
          });
   
          var templatePicker = new TemplatePicker({
            featureLayers: templateLayers,
            grouping: true,
            rows: "auto",
            columns: 1
          }, "templateDiv");
          templatePicker.startup();
  
          var layers = arrayUtils.map(evt.layers, function(result) { 
   //alert(result.layer.name);
            return { featureLayer: result.layer };
          });
          var settings = {
            map: map,
            templatePicker: templatePicker,
   //geometryService:geometryService,
            layerInfos: layers
          };
  
          var params = {settings: settings};    
          var myEditor = new Editor(params,"editorDiv");
          //define snapping options
          myEditor.startup();
        }
 }
);






Outcomes