Select to view content in your preferred language

Alert to user when Address Locator returns nothing

899
7
05-25-2010 11:46 AM
by Anonymous User
Not applicable
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;
 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.
0 Kudos
7 Replies
derekswingley1
Deactivated User
Check candidates.length first. If it's greater than zero, process the results. If it == zero, show your alert.
0 Kudos
by Anonymous User
Not applicable
Check candidates.length first. If it's greater than zero, process the results. If it == zero, show your alert.


That's not working either. Also, it's rather confusing when there's candidate AND candidates with an S. Which do you mean, or do you know?
0 Kudos
derekswingley1
Deactivated User
I mean candidates because you posted this:
dojo.io.script.jsonp_dojoIoScript4._jsonpCallback( {"candidates" : []});


Basically, if the candidates array has any items, i.e. length > 0, the geocode returned results. If length == 0, the geocode did not return any results.
0 Kudos
derekswingley1
Deactivated User
Also, it's your code. You're free to name variables how you see fit.
0 Kudos
by Anonymous User
Not applicable
That's not working. Would it have anything to do with being nested inside a For...In statement?
Is there anyone who could help with this!?

    
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;
 if (candidates.length > 0) {
    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 if (candidates.length == 0) {
    show_zip_alert();
    }
 }   
map.setExtent(points.getExtent().expand(3));
}
0 Kudos
derekswingley1
Deactivated User
Geocode "380 New York St, Redlands, CA, 92373" and then try something that won't return any candidates like "aksdjwoekf". You'll see an alert box that says "No Candidates" when there are no results:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=7" />
    <title>Find Address</title>
    <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/2.0/js/dojo/dijit/themes/tundra/tundra.css">
    <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=2.0"></script>
    <script type="text/javascript">
      dojo.require("esri.map");
      dojo.require("esri.tasks.locator");

      var map, locator;

      function init() {
        var initExtent = new esri.geometry.Extent({"xmin":-13343554,"ymin":2967656,"xmax":-7473190,"ymax":5902838,"spatialReference":{"wkid":102100}});
        map = new esri.Map("map", { extent: initExtent});
        var tiledMapServiceLayer = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");
        map.addLayer(tiledMapServiceLayer);

        locator = new esri.tasks.Locator("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Locators/ESRI_Geocode_USA/GeocodeServer");
        dojo.connect(locator, "onAddressToLocationsComplete", showResults);
      }

      function locate() {
        map.graphics.clear();
        var add = dojo.byId("address").value.split(",");
        var address = {
          Address : add[0],
          City: add[1],
          State: add[2],
          Zip: add[3]
        };
        locator.addressToLocations(address,["Loc_name"]);
      }

      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_DIAMOND);
        symbol.setColor(new dojo.Color([255,0,0,0.75]));

        var points =  new esri.geometry.Multipoint(map.spatialReference);
        if (candidates.length > 0) {
            for (var i=0, il=candidates.length; i<il; i++) {
              candidate = candidates;
              if (candidate.score > 70) {
                var attributes = { address: candidate.address, score:candidate.score, locatorName:candidate.attributes.Loc_name };
                var geom = esri.geometry.geographicToWebMercator(candidate.location);
                var graphic = new esri.Graphic(geom, symbol, attributes, infoTemplate);
                map.graphics.add(graphic);
                map.graphics.add(new esri.Graphic(geom, new esri.symbol.TextSymbol(attributes.address).setOffset(0, 8)));
                points.addPoint(geom);
              }
            }
            map.setExtent(points.getExtent().expand(3));
        } else if (candidates.length == 0) {
            alert("No Candidates.");
        }
      }

      dojo.addOnLoad(init);
    </script>
  </head>
  <body class="tundra">
    Address : <input type="text" id="address" size="60" value="380 New York St, Redlands, CA, 92373" /> <i>(Address, City, State, Zip)</i>
    <input type="button" value="Locate" onclick="locate()" /><br />
    <br />
    <div id="map" style="width:1200px; height:600px; border:1px solid #000;"></div>
  </body>
</html>
0 Kudos
by Anonymous User
Not applicable
Thank you so much. Sorry to make you have to spell it out like that, but I really appreciate it. I see now that the else..if statement is wrapped around the outside of the for...in statement. I almost got there on my own, but you made it happen, thanks again!
0 Kudos