AnsweredAssumed Answered

How to fix this javascript to join two scripts to get the desired output?

Question asked by manjarigoyal on Dec 24, 2014
Latest reply on Dec 29, 2014 by NSager-esristaff

I wrote this script and used some online help but it's not working. This script should geocode the address and then draw a circle (radius specified)around the address and then upload the zip shapefile and pull the data within the circle. this script is able to geocode the address but put the symbol on the address but its not doing anything else. I am not sure what's wrong in this script. Attached is the screen shot of what I got so far.


<!DOCTYPE html>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <!--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>Select with feature layer</title>
    <link rel="stylesheet" href="">
    <link rel="stylesheet" href="">
      html, body, #mapDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        border-top: solid 1px #343642;
        border-left: solid 1px #343642;
        border-bottom: solid 1px #343642;
        border-right: solid 1px #343642;
        margin:0px 0px 5px 5px;
        color: #343642;
        font:100% Georgia,"Times New Roman",Times,serif;
        /*letter-spacing: 0.05em;*/

    <script src=""></script>
      var map, candidateLocation;
        "esri/map", "esri/request", "esri/geometry/scaleUtils", "esri/layers/FeatureLayer", "esri/tasks/locator",
        "esri/tasks/query", "esri/geometry/Circle",
        "esri/graphic", "esri/InfoTemplate", "esri/symbols/SimpleMarkerSymbol",
        "esri/symbols/SimpleLineSymbol", "esri/symbols/SimpleFillSymbol", "esri/renderers/SimpleRenderer",
        "esri/symbols/Font", "esri/symbols/TextSymbol",
        "dojo/_base/array", "dijit/registry",
        "esri/config", "esri/Color", "dojo/dom", "dojo/parser", "dojo/json", "dojo/_base/lang", "dijit/layout/BorderContainer", "dijit/layout/ContentPane",
        "dijit/form/Button", "dijit/form/Textarea", "dojo/on",
      ], function(
        Map, request, scaleUtils, FeatureLayer, Locator,
        Query, Circle,
        Graphic, InfoTemplate, SimpleMarkerSymbol,
        SimpleLineSymbol, SimpleFillSymbol, SimpleRenderer,
        Font, TextSymbol,
        arrayUtils, registry,
        esriConfig, Color, dom, parser, JSON, lang, on
      ) {
        // use a proxy page if a URL generated by this page is greater than 2000 characters
        // this should not be needed as nearly all query & select functions are performed on the client = "/proxy/";
    on(dom.byId("uploadForm"), "change", function (event) {
            var fileName =;

            if (sniff("ie")) { //filename is full path in IE so extract the file name
              var arr = fileName.split("\\");
              fileName = arr[arr.length - 1];
            if (fileName.indexOf(".zip") !== -1) {//is file a zip - if not notify user
            else {
              dom.byId('upload-status').innerHTML = '<p style="color:red">Add shapefile as .zip file</p>';
          map = new Map("mapDiv", {
          basemap: "streets",
          center: [-95.249, 38.954],
          zoom: 14,
          slider: false
        function generateFeatureCollection (fileName) {
            var name = fileName.split(".");
            //Chrome and IE add c:\fakepath to the value - we need to remove it
            //See this link for more info:
            name = name[0].replace("c:\\fakepath\\", "");
            dom.byId('upload-status').innerHTML = '<b>Loading </b>' + name;
            //Define the input params for generate see the rest doc for details
            var params = {
              'name': name,
              'targetSR': map.spatialReference,
              'maxRecordCount': 1000,
              'enforceInputFileSizeLimit': true,
              'enforceOutputJsonSizeLimit': true
            //generalize features for display Here we generalize at 1:40,000 which is approx 10 meters
            //This should work well when using web mercator.
            var extent = scaleUtils.getExtentForScale(map, 40000);
            var resolution = extent.getWidth() / map.width;
            params.generalize = true;
            params.maxAllowableOffset = resolution;
            params.reducePrecision = true;
            params.numberOfDigitsAfterDecimal = 0;
            var myContent = {
              'filetype': 'shapefile',
              'publishParameters': JSON.stringify(params),
              'f': 'json',
              'callback.html': 'textarea'
              url: portalUrl + '/sharing/rest/content/features/generate',
              content: myContent,
              form: dom.byId('uploadForm'),
              handleAs: 'json',
              load: lang.hitch(this, function (response) {
                if (response.error) {
                var layerName = response.featureCollection.layers[0];
                dom.byId('upload-status').innerHTML = '<b>Loaded: </b>' + layerName;
              error: lang.hitch(this, errorHandler)
          function errorHandler (error) {
            dom.byId('upload-status').innerHTML =
            "<p style='color:red'>" + error.message + "</p>";
          function addShapefileToMap (featureCollection) {
            var fullExtent;
            var layers = [];
            arrayUtils.forEach(featureCollection.layers, function (layer) {
              var infoTemplate = new InfoTemplate("CellTower", "${*}");
              var featureLayer = new FeatureLayer(layer, {
                infoTemplate: infoTemplate
              //associate the feature with the popup on click to enable highlight and zoom to
              featureLayer.on('click', function (event) {
              //change default symbol if desired. Comment this out and the layer will draw with the default symbology
              fullExtent = fullExtent ?
                fullExtent.union(featureLayer.fullExtent) : featureLayer.fullExtent;
            map.setExtent(fullExtent.expand(1.25), true);

            dom.byId('upload-status').innerHTML = "";
        locator = new Locator("");
        locator.on("address-to-locations-complete", showResults);
        registry.byId("locate").on("click", locate);
        var featureLayer = new FeatureLayer(layer,{
            infoTemplate: infoTemplate,
          function locate() {
          var address = {
          "SingleLine": dom.byId("address").value
          locator.outSpatialReference = map.spatialReference;
          var options = {
            address: address,
            outFields: ["Loc_name"]

        function showResults(evt) {
          var symbol = new SimpleMarkerSymbol();
          var infoTemplate = new InfoTemplate(
            "Address: ${address}<br />Score: ${score}<br />Source locator: ${locatorName}"
          symbol.setColor(new Color([153,0,51,0.75]));

          var geom;
          arrayUtils.every(evt.addresses, function(candidate) {
            if (candidate.score > 80) {
              candidateLocation = candidate.location;             
              var attributes = {
                address: candidate.address,
                score: candidate.score,
                locatorName: candidate.attributes.Loc_name
              geom = candidate.location;
              var graphic = new Graphic(geom, symbol, attributes, infoTemplate);
              //add a graphic to the map at the geocoded location
              return false; //break out of loop after one candidate with score greater  than 80 is found.
          if ( geom !== undefined ) {
            map.centerAndZoom(geom, 13);
        // selection symbol used to draw the selected cell tower points within the buffer polygon
        var symbol = new SimpleMarkerSymbol(
          new SimpleLineSymbol(
            new Color([247, 34, 101, 0.9]),
          new Color([207, 34, 171, 0.9])
        //make unselected features invisible
        var nullSymbol = new SimpleMarkerSymbol().setSize(0);
        featureLayer.setRenderer(new SimpleRenderer(nullSymbol));
        var circleSymb = new SimpleFillSymbol(
          new SimpleLineSymbol(
            new Color([105, 105, 105]),
          ), new Color([255, 255, 0, 0.25])
        var circle;

        //when the address point is there it create a buffer around the address point of the specified distance.
        function Buffer(evt){
          var radius = dom.byId("Radius").options[dom.byId("Radius").selectedIndex].value;
          circle = new Circle({
            center: evt,
            geodesic: true,
            radius: radius,
            radiusUnit: "esriMiles"
          var graphic = new Graphic(circle, circleSymb);
          var query = new Query();
          query.geometry = circle.getExtent();
          //use a fast bounding box query. will only go to the server if bounding box is outside of the visible map
          featureLayer.queryFeatures(query, selectInBuffer);

        function selectInBuffer(response){
          var feature;
          var features = response.features;
          var inBuffer = [];
          //filter out features that are not actually in buffer, since we got all points in the buffer's bounding box
          for (var i = 0; i < features.length; i++) {
            feature = features[i];
          var query = new Query();
          query.objectIds = inBuffer;
          //use a fast objectIds selection query (should not need to go to the server)
          featureLayer.selectFeatures(query, FeatureLayer.SELECTION_NEW);

  <body class="claro">
    <div id="mainWindow" data-dojo-type="dijit/layout/BorderContainer"
         data-dojo-props="design:'sidebar', gutters:false"
         style="width:100%; height:100%;">

      <div id="leftPanel"
        Enter an address then click the locate button to use a sample address locator to return the location for
        street addresses in the United States.

        <textarea id="address">47 New Scotland Avenue, Albany, NY 12208</textArea>

        <button id="locate" data-dojo-type="dijit/form/Button">Locate</button>
      Choose the radius in Miles
      <select id="Radius">
      <option selected="selected" value="1">1 Mile</option>
          <option value="5">5 Miles</option>
          <option value="10">10 Miles</option>

            Add a zipped shapefile to the map.</p><p>Visit the
            <a target='_blank' href="">Shapefiles</a> help topic for information and limitations.</p>
              <form enctype="multipart/form-data" method="post" id="uploadForm">
              <div class="field">
                  <label class="file-upload">
                      <span><strong>Add File</strong></span>
                      <input type="file" name="file" id="inFile" />
              <span class="file-upload-status" style="opacity:1;" id="upload-status"></span>
              <div id="fileInfo"> </div>
        <div id="mapDiv" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'center'">