Drawing with Feature Layer

2043
3
Jump to solution
01-16-2014 12:43 PM
JosephFedor
New Contributor
The person I???m designing a program for, would like a feature, he saw on a real estate website:  where the user can use drawing tools to create a polygon or circle, and then markers will appear within that region for certain locations.
 
To accomplish that, I looked at both the Drawing Tools and the Select with Feature Layer examples. 

I was able to get each example working independently (with some help from this forum), on my own server.  But creating something with both together has proved to be a challenge.  I took lines of code form the Feature Layer example and placed them within the Drawing tools example.  But I haven???t quite achieved the desired result, yet.  I can draw the shapes; but the markers don???t appear within them. 

The error I get is: 

TypeError: a is undefined
The resource from this URL is not text: http://js.arcgis.com/3.8/


There error comes from the last three lines of this function: 
[HTML]
        function addToMap(evt) {
          var symbol;
          toolbar.deactivate();
          map.showZoomSlider();
          switch (evt.geometry.type) {
            case "point":
            case "multipoint":
              symbol = new SimpleMarkerSymbol();
              break;
            case "polyline":
              symbol = new SimpleLineSymbol();
              break;
            default:
    symbol = new SimpleFillSymbol();
     break;
          }
          var graphic = new Graphic(evt.geometry, symbol);
          map.graphics.add(graphic);

    var params = new BufferParameters();
          params.geometries  = [ evt.mapPoint ];
    geometryService.buffer(params);

        }
[/HTML]

I realized it may be helpful to see this full page, I???m working on.  Here it is:

[HTML]
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width,user-scalable=no">
    <!--The viewport meta tag is used to improve the presentation and behavior of the samples
      on iOS devices-->
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
    <title>Maps Toolbar</title>
   
    <link rel="stylesheet" href="http://js.arcgis.com/3.8/js/dojo/dijit/themes/nihilo/nihilo.css">
    <link rel="stylesheet" href="http://js.arcgis.com/3.8/js/esri/css/esri.css">
    <style>
      html, body, #mainWindow {
        font-family: sans-serif;
        height: 100%;
        width: 100%;
      }
      html, body {
        margin: 0;
        padding: 0;
      }
      #header {
        height: 80px;
        overflow: auto;
        padding: 0.5em;
      }
    </style>
   
    <script src="http://js.arcgis.com/3.8/"></script>
    <script type="text/javascript">
      var map, toolbar, symbol, geomTask;

      require([
        "esri/map", "esri/layers/FeatureLayer",
        "esri/tasks/query", "esri/tasks/QueryTask",
        "esri/tasks/GeometryService", "esri/tasks/BufferParameters",
        "esri/toolbars/draw",
        "esri/graphic", "esri/InfoTemplate",

        "esri/symbols/SimpleMarkerSymbol",
        "esri/symbols/SimpleLineSymbol",
        "esri/symbols/SimpleFillSymbol",
  "esri/config", "dojo/_base/Color", "dojo/dom",
        "dojo/parser", "dijit/registry",

        "dijit/layout/BorderContainer", "dijit/layout/ContentPane",
        "dijit/form/Button",
 
  "dojo/domReady!"
      ], function(
        Map, FeatureLayer,
        Query, QueryTask,
        GeometryService, BufferParameters,
  Draw,
  Graphic, InfoTemplate,
        SimpleMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol,
  esriConfig, Color, dom,
        parser, registry
      ) {
    esriConfig.defaults.io.proxyUrl = "proxy.php";
        parser.parse();

        map = new Map("map", {
          basemap: "streets",
          center: [-111.972077,41.223601],
          zoom: 14,
    slider: false
        });
 
 
        //add the census block points in selection mode. Note that an info template has been defined so when
        //selected features are clicked a popup window will appear displaying the content defined in the info template.
        var featureLayer = new FeatureLayer("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/0",{
          mode: FeatureLayer.MODE_SELECTION,
          infoTemplate: new InfoTemplate("Block: ${BLOCK}", "${*}"),
          outFields: ["POP2000","HOUSEHOLDS","HSE_UNITS", "TRACT", "BLOCK"]
        });

        // selection symbol used to draw the selected census block points within the buffer polygon
        var symbol = new SimpleMarkerSymbol
  (
          SimpleMarkerSymbol.STYLE_CIRCLE,
          12,
          new SimpleLineSymbol(
            SimpleLineSymbol.STYLE_NULL,
            new Color([247, 34, 101, 0.9]),
            1
          ),
          new Color([207, 34, 171, 0.5])
        );
        featureLayer.setSelectionSymbol(symbol);
        map.addLayer(featureLayer);

        // geometry service that will be used to perform the buffer
        var geometryService = new GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
 
  geometryService.on("buffer-complete", function(result)
  {
          map.graphics.clear();
          // draw the buffer geometry on the map as a map graphic
          var symbol = new SimpleFillSymbol(
            SimpleFillSymbol.STYLE_NULL,
            new SimpleLineSymbol(
              SimpleLineSymbol.STYLE_SHORTDASHDOTDOT,
              new Color([105,105,105]),
              2
            ),new Color([255,255,0,0.25])
          );
          var bufferGeometry = result.geometries[0]
          var graphic = new Graphic(bufferGeometry, symbol);
          map.graphics.add(graphic);

          //Select features within the buffered polygon. To do so we'll create a query to use the buffer graphic
          //as the selection geometry.
          var query = new Query();
          query.geometry = bufferGeometry;
          featureLayer.selectFeatures(query, FeatureLayer.SELECTION_NEW, function(results)
    {});
        });
      
      
   
     map.on("load", createToolbar);

        // loop through all dijits, connect onClick event
        // listeners for buttons to activate drawing tools
        registry.forEach(function(d) {
          // d is a reference to a dijit
          // could be a layout container or a button
          if ( d.declaredClass === "dijit.form.Button" ) {
            d.on("click", activateTool);
          }
        });

        function activateTool() {
          var tool = this.label.toUpperCase().replace(/ /g, "_");
          toolbar.activate(Draw[tool]);
          map.hideZoomSlider();
        }

        function createToolbar(themap) {
          toolbar = new Draw(map);
          toolbar.on("draw-end", addToMap);
        }


        function addToMap(evt) {
          var symbol;
          toolbar.deactivate();
          map.showZoomSlider();
          switch (evt.geometry.type) {
            case "point":
            case "multipoint":
              symbol = new SimpleMarkerSymbol();
              break;
            case "polyline":
              symbol = new SimpleLineSymbol();
              break;
            default:
    symbol = new SimpleFillSymbol();
     break;
          }
          var graphic = new Graphic(evt.geometry, symbol);
          map.graphics.add(graphic);

    var params = new BufferParameters();
          params.geometries  = [ evt.mapPoint ];
    geometryService.buffer(params);

        }
      });
    </script>
  </head>
  <body class="nihilo">

  <div id="mainWindow" data-dojo-type="dijit/layout/BorderContainer" data-dojo-props="design:'headline'">
    <div id="header" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'top'">
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
           <tr>
             <td>
                   <span>Draw:<br /></span>
      <button data-dojo-type="dijit/form/Button">Polygon</button>
      <button data-dojo-type="dijit/form/Button">Freehand Polygon</button>
      <!--The Arrow,Triangle,Circle and Ellipse types all draw with the polygon symbol-->
      <button data-dojo-type="dijit/form/Button">Circle</button>
      <button data-dojo-type="dijit/form/Button">Ellipse</button>
             </td>
             <td><p align="right"><a href="commercial_home.php"><img src="image/home.jpg" width="40" height="32" alt="Home" title="Home"></a>    <a href="logout.php"><img src="image/exit.png" width="40" height="32" alt="Exit" title="Exit"></a>    </p></td>
            </tr>
            </table>
    </div>
    <div id="map" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'center'"></div>
  </div>

  </body>
</html>
[/HTML]

Thanks,
jfedor
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
Hi Joseph,

To build off Robert's suggestion, you will also need to add the parameter distances and unit properties.

var params = new BufferParameters(); params.geometries  = [ evt.geometry]; params.distances = [ 1 ]; params.unit = GeometryService.UNIT_STATUTE_MILE; geometryService.buffer(params);

View solution in original post

0 Kudos
3 Replies
RobertScheitlin__GISP
MVP Emeritus
Joseph,

   The evt has a geometry but does not have mapPoint so shouldn't you be doing:

params.geometries  = [ evt.geometry ];
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi Joseph,

To build off Robert's suggestion, you will also need to add the parameter distances and unit properties.

var params = new BufferParameters(); params.geometries  = [ evt.geometry]; params.distances = [ 1 ]; params.unit = GeometryService.UNIT_STATUTE_MILE; geometryService.buffer(params);
0 Kudos
JosephFedor
New Contributor
You both are awesome, thank you so much. 

Robert I copied your code verbatim and it works!  The dots appear inside the area.  (The area would grow at first after I drew the polygon, so I adjusted the params distance to 0.01, instead of 1.)  It works perfect now; just like I thought it should. 

Thanks so very much,
jfedor
0 Kudos