Select to view content in your preferred language

ArcGIS Javascript API: Feature selection using graphics

7487
3
Jump to solution
09-29-2014 01:37 AM
HabG_
by
Occasional Contributor

Hi All,

I have a web map similar to the one in the code below. There are various option to draw a graphic and select the features inside the polygon. The style of my web map is organised in such away that, where the map is in the center and other functions such as legend in the right pane. However, when I move the graphic buttons to the right pane, the graphic polygon drawing and selection of features is not working. Am I missing something? anyone who can help me fix this, please paste the correct code if you can. Thanks.

  1. <!DOCTYPE html> 
  2. <html> 
  3.   <head> 
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"
  5.  
  6.     <!--The viewport meta tag is used to improve the presentation and behavior of the samples  
  7.       on iOS devices--> 
  8.     <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"
  9.     <title>Shapes and Symbols</title> 
  10.  
  11.     <link rel="stylesheet" href="http://js.arcgis.com/3.10/js/esri/css/esri.css"
  12.  
  13.     <style> 
  14.       #info { 
  15.         top: 20px; 
  16.         color: #444
  17.         height: auto; 
  18.         font-family: arial; 
  19.         right: 20px; 
  20.         margin: 5px; 
  21.         padding: 10px; 
  22.         position: absolute; 
  23.         width: 115px; 
  24.         z-index: 40
  25.         border: solid 2px #666
  26.         border-radius: 4px; 
  27.         background-color: #fff; 
  28.       } 
  29.       html, body, #mapDiv { 
  30.         padding:0
  31.         margin:0
  32.         height:100%; 
  33.       } 
  34.       button { 
  35.         display: block; 
  36.       } 
  37.     </style> 
  38.  
  39.     <script src="http://js.arcgis.com/3.10/"></script> 
  40.     <script> 
  41.       var map, tb; 
  42.  
  43.       require([ 
  44.         "esri/map", "esri/toolbars/draw", "dojo/promise/all"
  45.         "esri/symbols/SimpleMarkerSymbol", "esri/symbols/SimpleLineSymbol", "esri/symbols/SimpleFillSymbol"
  46.         "esri/symbols/PictureFillSymbol", "esri/symbols/CartographicLineSymbol", "esri/layers/FeatureLayer",  
  47.         "esri/graphic", "esri/tasks/query", "esri/tasks/QueryTask", "esri/InfoTemplate"
  48.         "esri/Color", "dojo/dom", "dojo/on", "dojo/domReady!" 
  49.       ], function
  50.         Map, Draw, All, 
  51.         SimpleMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol, 
  52.         PictureFillSymbol, CartographicLineSymbol, FeatureLayer, 
  53.         Graphic, Query, QueryTask, InfoTemplate, 
  54.         Color, dom, on 
  55.       ) { 
  56.         map = new Map("mapDiv", { 
  57.           basemap: "streets"
  58.           center: [-85.82966, 33.666494], 
  59.           zoom: 13 
  60.         }); 
  61.         map.on("load", initToolbar); 
  62.          
  63.         var featureLayer1 = new FeatureLayer("http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/0",{ 
  64.           mode: FeatureLayer.MODE_SELECTION, 
  65.           infoTemplate: new InfoTemplate("Block: ${BLOCK}", "${*}"), 
  66.           outFields: ["POP2000","HOUSEHOLDS","HSE_UNITS", "TRACT", "BLOCK"
  67.         }); 
  68.          
  69.         var featureLayer2 = new FeatureLayer("http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/1",{ 
  70.           mode: FeatureLayer.MODE_SELECTION, 
  71.           infoTemplate: new InfoTemplate("Block Group: ${BLKGRP}", "${*}"), 
  72.           outFields: ["POP2000","HOUSEHOLDS","HSE_UNITS", "TRACT", "BLKGRP"
  73.         }); 
  74.          
  75.         var featureLayer3 = new FeatureLayer("http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/2",{ 
  76.           mode: FeatureLayer.MODE_SELECTION, 
  77.           infoTemplate: new InfoTemplate("County Name: ${NAME}", "${*}"), 
  78.           outFields: ["POP2000","HOUSEHOLDS","HSE_UNITS","NAME"
  79.         }); 
  80.          
  81.         map.addLayers([featureLayer1, featureLayer2, featureLayer3]); 
  82.  
  83.         // markerSymbol is used for point and multipoint, see http://raphaeljs.com/icons/#talkq for more examples 
  84.         var markerSymbol = new SimpleMarkerSymbol(); 
  85.         markerSymbol.setPath("M16,4.938c-7.732,0-14,4.701-14,10.5c0,1.981,0.741,3.833,2.016,5.414L2,25.272l5.613-1.44c2.339,1.316,5.237,2.106,8.387,2.106c7.732,0,14-4.701,14-10.5S23.732,4.938,16,4.938zM16.868,21.375h-1.969v-1.889h1.969V21.375zM16.772,18.094h-1.777l-0.176-8.083h2.113L16.772,18.094z"); 
  86.         markerSymbol.setColor(new Color("#00FFFF")); 
  87.  
  88.         // lineSymbol used for freehand polyline, polyline and line.  
  89.         var lineSymbol = new CartographicLineSymbol( 
  90.           CartographicLineSymbol.STYLE_SOLID, 
  91.           new Color([255,0,0]), 10,  
  92.           CartographicLineSymbol.CAP_ROUND, 
  93.           CartographicLineSymbol.JOIN_MITER, 5 
  94.         ); 
  95.  
  96.         // fill symbol used for extent, polygon and freehand polygon 
  97.         var fillSymbol = new SimpleFillSymbol(); 
  98.  
  99.         function initToolbar() { 
  100.           tb = new Draw(map); 
  101.           tb.on("draw-end", addGraphic); 
  102.  
  103.           // event delegation so a click handler is not 
  104.           // needed for each individual button 
  105.           on(dom.byId("info"), "click", function(evt) { 
  106.             if ( evt.target.id === "info" ) { 
  107.               return
  108.             } 
  109.             var tool = evt.target.id.toLowerCase(); 
  110.             map.disableMapNavigation(); 
  111.             tb.activate(tool); 
  112.           }); 
  113.         } 
  114.  
  115.         function addGraphic(evt) { 
  116.           //deactivate the toolbar and clear existing graphics  
  117.           tb.deactivate();  
  118.           map.enableMapNavigation(); 
  119.  
  120.           // figure out which symbol to use 
  121.           var symbol; 
  122.           if ( evt.geometry.type === "point" || evt.geometry.type === "multipoint") { 
  123.             symbol = markerSymbol; 
  124.           } else if ( evt.geometry.type === "line" || evt.geometry.type === "polyline") { 
  125.             symbol = lineSymbol; 
  126.           } 
  127.           else
  128.             symbol = fillSymbol; 
  129.           } 
  130.  
  131.           map.graphics.add(new Graphic(evt.geometry, symbol)); 
  132.           queryMapService(evt.geometry); 
  133.         } 
  134.          
  135.         function queryMapService(Geom){ 
  136.           var promises = []; 
  137.            
  138.           var query = new Query(); 
  139.           query.returnGeometry = false;    
  140.           query.outFields = ["*"];    
  141.           query.geometry = Geom; 
  142.           promises.push(featureLayer1.selectFeatures(query, FeatureLayer.SELECTION_NEW)); 
  143.           promises.push(featureLayer2.selectFeatures(query, FeatureLayer.SELECTION_NEW)); 
  144.           promises.push(featureLayer3.selectFeatures(query, FeatureLayer.SELECTION_NEW)); 
  145.           var allPromises = new All(promises); 
  146.           allPromises.then(function (r) { showResults(r); }); 
  147.         } 
  148.          
  149.         function showResults(results) { 
  150.           var resultCount = results.length; 
  151.           console.log(resultCount); 
  152.         } 
  153.       }); 
  154.     </script> 
  155.   </head> 
  156.    
  157.   <body> 
  158.      
  159.     <div id="info"
  160.       <div>Select a shape then draw on map to add graphic</div> 
  161.       <button id="Point">Point</button> 
  162.       <button id="Multipoint">Multipoint</button> 
  163.       <button id="Line">Line</button> 
  164.       <button id="Polyline">Polyline</button> 
  165.       <button id="FreehandPolyline">Freehand Polyline</button> 
  166.       <button id="Triangle">Triangle</button> 
  167.       <button id="Extent">Rectangle</button> 
  168.       <button id="Circle">Circle</button> 
  169.       <button id="Ellipse">Ellipse</button> 
  170.       <button id="Polygon">Polygon</button> 
  171.       <button id="FreehandPolygon">Freehand Polygon</button> 
  172.     </div> 
  173.  
  174.     <div id="mapDiv"></div> 
  175.  
  176.   </body> 
  177. </html>
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

Hi Hab,

This should work once you add the graphic buttons to a side panel.  You will want to make sure you are calling the dijit modules and dojo/parser.  Take a look at the following example here.

View solution in original post

3 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Hab,

This should work once you add the graphic buttons to a side panel.  You will want to make sure you are calling the dijit modules and dojo/parser.  Take a look at the following example here.

HabG_
by
Occasional Contributor

Thanks Jake.

0 Kudos
RichardMoussopo
Occasional Contributor III

This is very handy,

Thank you Jake

0 Kudos