Select to view content in your preferred language

What is the syntax for testing number of results from a geocoder?

1367
6
Jump to solution
06-18-2014 10:22 AM
MatthewBaker2
Deactivated User
Hi all,

I'm trying to test the length of evt.addresses from the results of a geocoder task.

I can get a response if
evt.addresses.length > 1
, but I can't figure out how to test if there are no results, that is,
evt.addresses.length === 0
.

Any thoughts on the correct syntax to use to check for NO results?

Thanks!

-m
0 Kudos
1 Solution

Accepted Solutions
TimWitt
Deactivated User
Matt,

maybe this helps?

I use this

        geocoder.on("find-results", showAlert);                   // If no address found         function showAlert(evt) {               if (evt.results.results.length > 0){                 console.log("Match Found");           } else {             alert("No address has been found.");           }         };


Hope this is what you were looking for.

Tim

View solution in original post

0 Kudos
6 Replies
TimWitt
Deactivated User
Matt,

maybe this helps?

I use this

        geocoder.on("find-results", showAlert);                   // If no address found         function showAlert(evt) {               if (evt.results.results.length > 0){                 console.log("Match Found");           } else {             alert("No address has been found.");           }         };


Hope this is what you were looking for.

Tim
0 Kudos
MatthewBaker2
Deactivated User
Hey Tim,

That does help - thank you!

But - for some reason I get a different response with my locator, so I'm still stuck...

Using the Firefox developer / network tools, the sample you provided gives back this with no results:

dojo.io.script.jsonp_dojoIoScript10._jsonpCallback({"results":[]});


With my locator, I get back this:

[ATTACH=CONFIG]34695[/ATTACH]

Do you know what that difference is and why the IF/ELSE statement would treat it differently?

Thanks again!
0 Kudos
TimWitt
Deactivated User
Can you post your whole code by chance?
0 Kudos
MatthewBaker2
Deactivated User
Here's what I've got, with some stuff you might not need:

function main() {

 var map, locator, arcLocator;

 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 : [-104.983492, 39.738346],
   zoom : 13
  });

  locator = new Locator("http://arcgisdev01:6080/arcgis/rest/services/Locators/DPS_Geocoder_Dev/GeocodeServer");

  locator.on("address-to-locations-complete", showResults);

  arcLocator = new Locator("http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer");
  arcLocator.on("address-to-locations-complete", showArcResults);

  // listen for button click then geocode
  registry.byId("locate").on("click", locate);

  map.infoWindow.resize(200, 125);

  function locate() {
   map.graphics.clear();
   var address = {
    "SingleKey" : dom.byId("address").value
   };

   
   
   locator.outSpatialReference = new esri.SpatialReference({
   
    wkid : 4326

   });

   var options = {
    address : address,
    outFields : ["Match_addr"],
    //autoComplete : true,
   };
   locator.addressToLocations(options);

  }

  function arcLocate(city) {

   //alert("new city is: " + city);

   var entryCity = city;
   var entryAddress = dom.byId("address").value;
   var arcAddress = entryAddress + ", " + city + ", CO";
   var arcEntryAddress = {
    "SingleKey" : arcAddress
   };

   arcLocator.outSpatialReference = new esri.SpatialReference({
    wkid : 4326
   });
   var options = {
    address : arcEntryAddress,
    outFields : ["*"]
   };

   arcLocator.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_CIRCLE);
   symbol.setColor(new Color([5, 151, 255, 0.75]));

   var geom;
   arrayUtils.every(evt.addresses, function(candidate) {
    console.log(candidate.score);

    if (evt.addresses.length >= 1) {

     alert('there were results');

    }
    
    else {
     
     alert('there were no results');
    }
    
     /*
     else if (candidate.score > 96) {
          console.log(candidate.location);
          var attributes = {
           address : candidate.address,
           score : candidate.score,
           locatorName : candidate.attributes.Loc_name
          };
          geom = candidate.location;
                //shrink the map window
          //mapDiv = document.getElementById('map');
          //mapDiv.style.height = "200px";
          //end shrink map window
                //add a graphic to the map at the geocoded location
          var graphic = new Graphic(geom, symbol, attributes, infoTemplate);
          map.graphics.add(graphic);
          //add a text symbol to the map listing the location of the matched address.
          var displayText = "You Live: " + candidate.address;
          var font = new Font("12pt", Font.STYLE_NORMAL, Font.VARIANT_NORMAL, Font.WEIGHT_BOLD, "Helvetica");
          var textSymbol = new TextSymbol(displayText, font, new Color("#0597FF"));
          textSymbol.setOffset(0, 15);
                map.graphics.add(new Graphic(geom, textSymbol));
                fillDetails(candidate.address);
          //get boundary query
          //queryBounds(geom);
                //alert('results: ' + evt.addresses.length);
                return false;
                //break out of loop after one candidate with score greater  than 80 is found.
               } else {
                var entryCity = dom.byId("city").value;
          alert('score < 96 going to use 2nd geocoder with : ' + entryCity);
                arcLocate(entryCity);
               }*/
     

   });

   if (geom !== undefined) {
    map.centerAndZoom(geom, 16);

   }
  }

  function showArcResults(evt) {
   alert(AddressCandidate.length);
  }

  function fillDetails(candAddr) {
   //alert("candAddr: " + candAddr);

   var addr;
   addr = document.getElementById("userAddress");
   document.getElementById("boundaryInfo").style.visibility = "visible";
   addr.innerHTML = candAddr;

   var year;
   year = document.getElementById("schoolYear");
   //get text rather than value for display purposes
   userYear.innerHTML = year.options[year.selectedIndex].text;

   var grade;
   grade = document.getElementById("schoolGrade");
   //get text rather than value for display purposes
   //value would feed into query
   userGrade.innerHTML = grade.options[grade.selectedIndex].text;

  }

  function queryBounds(geom) {
   //report geometry X,Y of the locator result
   //alert(geom.x);
   //alert(geom.y);

   var grade;
   grade = document.getElementById("schoolGrade");
   var entryGrade;
   entryGrade = grade.options[grade.selectedIndex].value;

   var year;
   year = document.getElementById("schoolYear");
   var entryYear;
   entryYear = year.options[year.selectedIndex].value;

   //alert("Grade: " + entryGrade + "Year layer: " + entryYear);

  }

 });
}

0 Kudos
TimWitt
Deactivated User
Matt, I don't know why this error pops-up, sorry 😞

I hope somebody else can help you.
0 Kudos
MatthewBaker2
Deactivated User
Well thanks for the correct syntax!!!


Matt, I don't know why this error pops-up, sorry 😞

I hope somebody else can help you.
0 Kudos