Error handling with addressToLocations

803
3
02-06-2013 07:03 AM
Peted_Oronzio
New Contributor
I'm using locator.addressToLocations to geocode an intersection name. My code is working great provided that the intersection name is valid and exists. However, if the address in invalid, the locator simply strips the address and geocodes the city and state. I'd like to catch these cases and handle them myself. (i.e. "Broadway & NoName St, Denver, CO could not be found") I can certainly identify cases where the address is empty in the candidate, but I can't find any reference to the original address I was trying to geocode while I'm in the showResults() function.

Suggestions?
Thanks,
Pete

relevant snippet:

function showAddress(aAddress, aWeight, aText) {
    lLocator.outSpatialReference = lMap.spatialReference;
    var options = {
        address:{ "SingleLine":aAddress },
        outFields:["Loc_name","Match_addr"]
    }
    lLocator.addressToLocations(options);
}

function showResults(candidates) {
    try {
        var candidate;
        dojo.every(candidates, function (candidate) {
            console.log(candidate.score + "|" + candidate.attributes.Match_addr);

            // Right here, I'd like to say, if (candidate.address == ""){alert("Bad address: " + candidate.attributes.Request_Address);}
            // But Request_Address isn't something that exists.  How do I get it?
            if (candidate.score > 80) {
              ...
}

function initMap() {
    lMap = new esri.Map("MyMapDiv", {
        basemap:"streets", center:[-93.5, 41.431], zoom:5
    });

    lLocator = new esri.tasks.Locator("http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer");
    dojo.connect(lLocator, "onAddressToLocationsComplete", showResults);
    showAddress( "Broadway & NoName St, Denver, CO", 30, "This is the spot" );
}

0 Kudos
3 Replies
JohnGravois
Frequent Contributor
this obviously wouldn't work when passing hardcoded addresses like in your example, but in a real app, couldn't you pull the information directly from the textbox?

console.log("Bad address: " + dojo.byId("address").value);


also, are you getting results from our world geocoding service for just city and state when you geocode "Broadway & NoName St, Denver, CO"?  i get zero candidates.
0 Kudos
Peted_Oronzio
New Contributor
this obviously wouldn't work when passing hardcoded addresses like in your example, but in a real app, couldn't you pull the information directly from the textbox?

console.log("Bad address: " + dojo.byId("address").value);


also, are you getting results from our world geocoding service for just city and state when you geocode "Broadway & NoName St, Denver, CO"?  i get zero candidates.


I have the same question, except I am geocoding a few locations in a row, and the data isn't on the page.  The geocoding process uses a callback, so I haven't been able to find a way to pass data along with it.  (even just a simple id would be great)  Now I'm just throwing it over the fence and hoping it works. 

You know... I've been assuming the callback is asynchronous.  If it is synchronous, maybe I can set a global variable during the process.  But in that case, I don't understand why it would be a callback.  I figured it was to optimize the process by being asynchronous.

Ideas?
Thanks,
Pete
0 Kudos
JohnGravois
Frequent Contributor
i had to get some help from the awesome @MattDriscoll on this tricky (and interesting) question.

it is possible to dig out your request parameters from the dojo.Deferred returned by the method (rather than relying on the response object which filters out just results). 

this is true not just for .addressToLocations(), but all asynchronous requests built upon the more generic esri.request().

var promise = locator.addressToLocations(options);

promise.then(function(response) {
    console.log(promise._pendingDfd.ioArgs.args.content.SingleLine + ' wasnt a cool address.');
});
0 Kudos