How to test the result of a Locator for NO results?

783
6
Jump to solution
06-18-2014 11:50 AM
MatthewBaker2
Occasional Contributor II
All,

I'm having a hard time figuring out how to test a Locator (not a geocoder) for NO results.

Using evt.addresses.length, I can get the value of 0 for a Locator with no results.

However, when trying to test for no results in an IF/ELSE statement, I am not getting any response.

Here is the IF/ELSE statement I'm using:

if (evt.addresses.length > 0) {       alert('there were results');      }          else {            alert('there were no results');     }


When I test with my Locator, or the World Locator hosted by ESRI used in the sample below, I get no response in the ELSE statement.


To test this yourself:

- use the Sandbox for the "geocode an address" sample,

- In the javascript, below line 102 right below geom = candidate.location;, stick in the following:

alert('results: ' + evt.addresses.length);

- click the 'run' button

- type in a valid address - you should see the # of results pop up

- type in some gibberish, (the locator is good at finding a result for almost anything that ISN'T gibberish). You'll see the 'results: 0'

In the Firefox developer tools, this is what the result looks like:

[ATTACH=CONFIG]34699[/ATTACH]

Any thoughts?

Thank you!!!
0 Kudos
1 Solution

Accepted Solutions
JonathanUihlein
Esri Regular Contributor
Your if/else is inside your loop that uses evt.addresses as the parameter.

Since evt.addresses doesn't have any candidates when there are no results, the loop never fires.

evt.addresses.length will always be greater than 1 inside your loop.

var h = 0;  for( i = 0, i < h, i++){ //code will never get here }

View solution in original post

0 Kudos
6 Replies
JonathanUihlein
Esri Regular Contributor
Hello. Have you tried something like:


if(evt.addresses){
 //results exist
}else{
 //do nothing
}
0 Kudos
MatthewBaker2
Occasional Contributor II
Seems to recognize a result ok, but doesn't recognize 'no' results...

Thank you!!!
0 Kudos
JonathanUihlein
Esri Regular Contributor
Can you post more of your specific code?

I was able to successfully check for no results using the provided sample and an if statement.

See attached screenshot.
0 Kudos
MatthewBaker2
Occasional Contributor II
Hi there,

I see its writing to the console, but can you get an alert to show in the ELSE statement if there is no result? That's what I'm struggling with.

Here's code I can't get to work that I wrote in the sandbox:

<!DOCTYPE html>
<html>
  <head>
    <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>Find Address</title>

    <link rel="stylesheet" href="http://js.arcgis.com/3.9/js/dojo/dijit/themes/claro/claro.css">
    <link rel="stylesheet" href="http://js.arcgis.com/3.9/js/esri/css/esri.css">
    <style>
      html, body { 
        height: 100%; width: 100%;
        margin: 0; padding: 0;
      } 
      #map{ 
        padding:0;
        border:solid 1px #343642;
        margin:5px 5px 5px 0px;
      }
      #leftPane{
        width:20%;
        border-top: solid 1px #343642;
        border-left: solid 1px #343642;
        border-bottom: solid 1px #343642;
        margin:5px 0px 5px 5px;
        color: #343642;
        font:100% Georgia,"Times New Roman",Times,serif;
        /*letter-spacing: 0.05em;*/
      }
     </style>

    <script src="http://js.arcgis.com/3.9/"></script>
    <script>
      var map, locator;

      require([
        "esri/map", "esri/tasks/locator", "esri/graphic",
        "esri/InfoTemplate", "esri/symbols/SimpleMarkerSymbol",
        "esri/symbols/Font", "esri/symbols/TextSymbol",
        "dojo/_base/array", "esri/Color",
        "dojo/number", "dojo/parser", "dojo/dom", "dijit/registry",

        "dijit/form/Button", "dijit/form/Textarea",
        "dijit/layout/BorderContainer", "dijit/layout/ContentPane", "dojo/domReady!"
      ], function(
        Map, Locator, Graphic,
        InfoTemplate, SimpleMarkerSymbol, 
        Font, TextSymbol,
        arrayUtils, Color,
        number, parser, dom, registry
      ) {
        parser.parse();

        map = new Map("map", { 
          basemap: "streets",
          center: [-93.5, 41.431],
          zoom: 5
        });
        
        locator = new Locator("http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer");
        locator.on("address-to-locations-complete", showResults);

        // listen for button click then geocode
        registry.byId("locate").on("click", locate);
        
        map.infoWindow.resize(200,125);

        function locate() {
          map.graphics.clear();
          var address = {
            "SingleLine": dom.byId("address").value
          };
          locator.outSpatialReference = map.spatialReference;
          var options = {
            address: address,
            outFields: ["Loc_name"]
          }
          locator.addressToLocations(options);
        }

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

          var geom;
          arrayUtils.every(evt.addresses, function(candidate) {
            console.log(candidate.score);
            // NEW 
            if (evt.addresses.length > 0) {
              console.log(candidate.location);
              var attributes = { 
                address: candidate.address, 
                score: candidate.score, 
                locatorName: candidate.attributes.Loc_name 
              };   
              alert('results!');
              geom = candidate.location;
              var graphic = new Graphic(geom, symbol, attributes, infoTemplate);
              //add a graphic to the map at the geocoded location
              map.graphics.add(graphic);
              //add a text symbol to the map listing the location of the matched address.
              var displayText = candidate.address;
              var font = new Font(
                "16pt",
                Font.STYLE_NORMAL, 
                Font.VARIANT_NORMAL,
                Font.WEIGHT_BOLD,
                "Helvetica"
              );
             
              var textSymbol = new TextSymbol(
                displayText,
                font,
                new Color("#666633")
              );
              textSymbol.setOffset(0,8);
              map.graphics.add(new Graphic(geom, textSymbol));
              return false; //break out of loop after one candidate with score greater  than 80 is found.
            }
            
            //NEW
            else {
              
              alert('no results');
              
            }
          });
          if ( geom !== undefined ) {
            map.centerAndZoom(geom, 12);
          }
        }
      });
    </script>
  </head>
  <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="leftPane"
           data-dojo-type="dijit/layout/ContentPane" 
           data-dojo-props="region:'left'">
        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. 
        <br>

        <textarea type="text" id="address"/>380 New York St, Redlands</textArea>
        <br>

        <button id="locate" data-dojo-type="dijit/form/Button">Locate</button> 
      </div>

      <div id="map" 
           data-dojo-type="dijit/layout/ContentPane" 
           data-dojo-props="region:'center'">
      </div>
    </div>
  </body>
</html>
0 Kudos
JonathanUihlein
Esri Regular Contributor
Your if/else is inside your loop that uses evt.addresses as the parameter.

Since evt.addresses doesn't have any candidates when there are no results, the loop never fires.

evt.addresses.length will always be greater than 1 inside your loop.

var h = 0;  for( i = 0, i < h, i++){ //code will never get here }
0 Kudos
MatthewBaker2
Occasional Contributor II
Ahhhh fantastic! That is it!

Many many thanks!!!

-m



Your if/else is inside your loop that uses evt.addresses as the parameter.

Since evt.addresses doesn't have any candidates when there are no results, the loop never fires.

evt.addresses.length will always be greater than 1 inside your loop.

var h = 0;

for( i = 0, i < h, i++){
//code will never get here
}
0 Kudos