Tell me more ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

How can I write a code for checking the error condition in Javascript (ArcGIS) for addressToLocations(params, callback?, errback?).

if somebody entered an invalid address- how can I call error function? Please share your thoughts!

my code below:-

-->

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<title>Find Address</title>
<link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.2/js/dojo/dijit/themes/claro/claro.css">
<link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.2/js/esri/css/esri.css" />



<script type="text/javascript">
  var djConfig = {
    parseOnLoad: true
  };
</script>

<script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=3.2"></script>
<script type="text/javascript">
  dojo.require("esri.tasks.locator");
  dojo.require("dojo.number");
  dojo.require("dijit.form.Button");
  dojo.require("dijit.form.Textarea");



  var map, locator;

  function init() {
    locator = new esri.tasks.Locator("http://arc-gis-1/ArcGIS/rest/services/Essential/AddressLoc/GeocodeServer");
    dojo.connect(locator, "onAddressToLocationsComplete", showResults);

  }
  function clearContents(element) {
    element.value = '';
    }

  function locate() {
    dojo.empty("messages");
    dojo.empty("content");
    dojo.empty("candScore");
    var address = {"Single Line Input":dojo.byId("address").value};
    var options = {
      address:address,
      outFields:["Loc_name"]
    }
    locator.addressToLocations(options);
  }

  function showResults(candidates) {
    var candidate;
    var symbol = new esri.symbol.SimpleMarkerSymbol();
    var infoTemplate = new esri.InfoTemplate("Location", "Address: ${address}<br />Score: ${score}<br />Source locator: ${locatorName}");
    symbol.setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE);
    symbol.setColor(new dojo.Color([153,0,51,0.75]));
    var geom;
   // alert(candidates[0].score);
    if (candidates.attribute==undefined)
    {
        var content = "That doesn't seem to be a valid address! ";
        dojo.byId('content').innerHTML = content;
    }

    dojo.every(candidates,function(candidate){
    alert(candidate.score);
      //console.log(candidate.score);
      var candScore= "Score =" + candidate.score;

      if (candidate.score > 80) {
        console.log(candidate.location);
        var attributes = { address: candidate.Match_addr, score:candidate.score, locatorName:candidate.attributes.Loc_name };   
        geom = candidate.location;
        var graphic = new esri.Graphic(geom, symbol, attributes, infoTemplate);
        var queryTaskTouches = new esri.tasks.QueryTask("http://arc-gis-1/ArcGIS/rest/services/Apps/Sheriff_ESN/MapServer/0");
        var query = new esri.tasks.Query();
        query.returnGeometry = false;
        query.outFields = ["*"];
        query.geometry = graphic.geometry;
        query.spatialRelationship = esri.tasks.Query.SPATIAL_REL_WITHIN;
        queryTaskTouches.execute(query);
        dojo.byId('messages').innerHTML = "<b>Executing Polygon Touches Query...</b>";

  dojo.connect(queryTaskTouches, "onComplete", function(fset) {

      var resultFeatures = fset.features;
      for (var i=0, il=resultFeatures.length; i<il; i++) {
        var querygraphic = resultFeatures[i];
        querygraphic.setSymbol(symbol);
        querygraphic.setInfoTemplate(infoTemplate);

      }
      var matchAdd = candidate.address;
      var ESN= querygraphic.attributes.ESNs;

   content = "<table border='1'><tr><th>Address</th><th>ESN</th></tr>"
                    +"<tr><td>"+  matchAdd +"</td><td>"+  ESN +"</td></tr>"
           "</table>";
      dojo.byId('messages').innerHTML = "";
      dojo.byId('content').innerHTML = content;
      dojo.byId('candScore').innerHTML = candScore;
    });



      }
      else
      {
      matchAdd="Not a Valid Address";
      var ESN= " ";
      var content = "<table border='1'><tr><th>Address</th><th>ESN</th></tr>"
                    +"<tr><td>"+  matchAdd +"</td><td>"+  ESN +"</td></tr>"
           "</table>";
      dojo.byId('messages').innerHTML = "";
      dojo.byId('content').innerHTML = content;
      }



    });

  }

  dojo.addOnLoad(init);
</script>

share|improve this question
Welcome to GIS SE! Can you tell us what you've tried so far? – R.K. Nov 30 '12 at 2:01
1  
Thanks. I am enclosing my code. Please see the "If statement" where I was trying to check "undefined". I am seeing that locator has a way of checking onError/errback?. But, I am not sure how to implement it:( – Ann Nov 30 '12 at 14:37

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.