bdebus

Alert to user when Address Locator returns nothing

Discussion created by bdebus on May 25, 2010
Latest reply on May 26, 2010 by bdebus
I'm using an Address Locator I made that's based on ZIP code polygons. I'm using the Geolocator sample from the JS API Samples website. When a user enters a zip other than what's in my dataset, nothing happens. I would like to have an alert box pop-up with a msg. I though this could be done using an if...else statement, but doesn't seem to be working. I've tried
else if (candidate.score != 100)
but that doesn't work. The way my locator is setup, the results are either a score of 100 or nothing. The results look like this when it works:
dojo.io.script.jsonp_dojoIoScript3._jsonpCallback({"candidates" : [{"address" : "48161","location" :{"x" : -9292144.7345,"y" : 5147689.1911},"score" : 100,"attributes" : {}}]});
Or like this when it doesn't:
dojo.io.script.jsonp_dojoIoScript4._jsonpCallback({"candidates" : []});
Could I use the onError(error) event? If so, how would I do that?
See below for the code in question:

//pop up alert msg
function show_zip_alert()
  {
  alert("Please eneter a valid ZIP Code");
  }

//Locator functions
function locate() {
        map.graphics.clear();
        map.infoWindow.hide();
        var add = dojo.byId("address").value.split(",");
        var address = {
          ZIP: add[0]
        };
        locator.addressToLocations(address);
      }

function showResults(candidates) {
        var candidate;
        var symbol = new esri.symbol.SimpleMarkerSymbol();

        symbol.setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_DIAMOND);
        symbol.setColor(new dojo.Color([0,0,0,0.8]));
        var points =  new esri.geometry.Multipoint(map.spatialReference);

        for (var i=0, il=candidates.length; i<il; i++) {
candidate = candidates[i];
if (candidate.score === 100) {
var attributes = { address:candidate.address, score:candidate.score, locatorName:candidate.attributes.Loc_name };
var graphic = new esri.Graphic(candidate.location, symbol, attributes);
map.graphics.add(graphic);
map.graphics.add(new esri.Graphic(candidate.location, new esri.symbol.TextSymbol(attributes.address).setOffset(0, 10)));
points.addPoint(candidate.location);
}
else
{
show_zip_alert();
}
     }  
map.setExtent(points.getExtent().expand(3));
}


It also seems I don't really need to use an array (and then have to loop through it) since I'm only using the ZIP field of the locator, but I don't really know how to remove that. Also I'm not showing the InfoWindow. I'm not really a developer, so any help would be greatly appreciated.

Outcomes