Synchronous communication while searching address over geocoding services

2856
2
Jump to solution
07-21-2014 10:48 AM
BhavinSanghani
Occasional Contributor II

I have one scenario for address search. Let's say I allow my customers to configure multiple geocoding services. If first one fails, then address search will be performed on second one. If second one doesn't return any result then go to third one and so on.

 

How can I code for this? I want to start searching on second geocode service only if first one returns no result. Is there any way to achieve this using Locator?

 

I know we can perform nested calls but for how many services deep - I won't have any idea. Also, all() won't be useful because it requires all deferred objects to complete. Any technique to achieve the scenario I mentioned?

0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

Hi Bhavin,

You should look at creating a Composite Address Locator.  If this is not an option, you can check the address candidate score, and if it is not greater than 0, you can execute another locator.  Ex:

locator = new Locator("http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer");

locator2 = new Locator(...);

locator3 = new Locator(...);

locator.on("address-to-locations-complete", function(results){

  arrayUtils.every(results.addresses, function(candidate) {

    console.log(candidate.score);

    if (candidate.score > 0) {

      console.log(candidate.location);

      var attributes = {

        address: candidate.address,

        score: candidate.score,

        locatorName: candidate.attributes.Loc_name

      };  

      ......            

      return false; //break out of loop after one candidate with score greater  than 80 is found.

    }

    else{

      locate2();

    }

   });

})

locator2.on("address-to-locations-complete", function(results){

  arrayUtils.every(results.addresses, function(candidate) {

    console.log(candidate.score);

    if (candidate.score > 0) {

      console.log(candidate.location);

      var attributes = {

        address: candidate.address,

        score: candidate.score,

        locatorName: candidate.attributes.Loc_name

      };  

      ......            

      return false; //break out of loop after one candidate with score greater  than 80 is found.

    }

    else{

      locate3();

    }

   });

})

locator3.on("address-to-locations-complete", function(results){

  ....

})

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 locate2() {

  map.graphics.clear();

  var address = {

    "SingleLine": dom.byId("address").value

  };

  locator2.outSpatialReference = map.spatialReference;

  var options = {

    address: address,

    outFields: ["Loc_name"]

  }

  locator2.addressToLocations(options);

}

function locate3(){

  ....

}

View solution in original post

0 Kudos
2 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Bhavin,

You should look at creating a Composite Address Locator.  If this is not an option, you can check the address candidate score, and if it is not greater than 0, you can execute another locator.  Ex:

locator = new Locator("http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer");

locator2 = new Locator(...);

locator3 = new Locator(...);

locator.on("address-to-locations-complete", function(results){

  arrayUtils.every(results.addresses, function(candidate) {

    console.log(candidate.score);

    if (candidate.score > 0) {

      console.log(candidate.location);

      var attributes = {

        address: candidate.address,

        score: candidate.score,

        locatorName: candidate.attributes.Loc_name

      };  

      ......            

      return false; //break out of loop after one candidate with score greater  than 80 is found.

    }

    else{

      locate2();

    }

   });

})

locator2.on("address-to-locations-complete", function(results){

  arrayUtils.every(results.addresses, function(candidate) {

    console.log(candidate.score);

    if (candidate.score > 0) {

      console.log(candidate.location);

      var attributes = {

        address: candidate.address,

        score: candidate.score,

        locatorName: candidate.attributes.Loc_name

      };  

      ......            

      return false; //break out of loop after one candidate with score greater  than 80 is found.

    }

    else{

      locate3();

    }

   });

})

locator3.on("address-to-locations-complete", function(results){

  ....

})

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 locate2() {

  map.graphics.clear();

  var address = {

    "SingleLine": dom.byId("address").value

  };

  locator2.outSpatialReference = map.spatialReference;

  var options = {

    address: address,

    outFields: ["Loc_name"]

  }

  locator2.addressToLocations(options);

}

function locate3(){

  ....

}

0 Kudos
BhavinSanghani
Occasional Contributor II

Problem with this approach is it's limited to 3 locators. I can't restrict customers to provide 3 geocoding services. I need to give an option of any no. of geocoding services they want to configure in their environment even it may not be practically true. 

0 Kudos