Locator not solving in right order

3441
11
07-08-2015 05:18 PM
HamishKingsbury1
Occasional Contributor

Hey there

I've a list of addresses that I want to solve, ive tried two things:

  1. using locator.addressToLocations - I iterate through the list of addresses. This will solve the results, but sometimes they will not be in the same order as they were in the list - this is an issue.
  2. using locator.addressessToLocations - this will simply not solve. I can't even get it to work in the REST end point.

I'd like to try to fix the issues with my first attempt as i also need to do some other things during the iteration. Below is my code:

    for (i in depotLocs) {
        console.log(i)
        var locator = new esri.tasks.Locator(this.config.geoCoder);
        console.log("A")
        locator.outSpatialReference = sRef;
        console.log("B")
        var optionsFrom = {
            address: { "SingleLine": depotLocs },
            outFields: ["Loc_name"]
        };
     
        locator.addressToLocations(optionsFrom,function(candidate){
            var r = candidate;
            facilitiesGraphicsLayer.add(new Graphic(new Point(r[0].location.x,r[0].location.y,sRef)));
            depotGraphic.push(new Graphic(new Point(r[0].location.x,r[0].location.y,sRef)));
            console.log(i)
            console.log(r[0].location.x)
            console.log(r[0].location.y)
            });

Does anyone have a possible solution to it being solved out of order? it is a bit of pain as it doesn't consistently happen.

Cheers

Hamish

Tags (1)
0 Kudos
11 Replies
RobertScheitlin__GISP
MVP Emeritus

Hamish,

  I am not real sure about the order issue but here are some other things I would change about your code:

No need to have the locator var inside the loop where it is recreated each time.

Use dojo for handling array looping.

You will need to require 'dojo/_base/array'.

If you have already required esri/tasks/Locator then do not use the Legacy "esri.tasks.Locator" when you you construct the var using new.

var locator = new Locator(this.config.geoCoder);
array.map(depotLocs, function(depotLoc){
  console.log(depotLoc);
  locator.outSpatialReference = sRef;
  var optionsFrom = {
    address: {"SingleLine": depotLoc},
    outFields: ["Loc_name"]
  };
  locator.addressToLocations(optionsFrom, function(candidate){
    var r = candidate;
    facilitiesGraphicsLayer.add(new Graphic(new Point(r[0].location.x, r[0].location.y, sRef)));
    depotGraphic.push(new Graphic(new Point(r[0].location.x, r[0].location.y, sRef)));
    console.log(i);
    console.log(r[0].location.x);
    console.log(r[0].location.y);
  });
}, this);

Let me know if this changes anything for you.

0 Kudos
HamishKingsbury1
Occasional Contributor

Unfortunately that's still returning them out of order... Its really bizarre.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Hamish,

  I think you need to use addressesToLocations. Here is a sample of how to use this method (untested):

    var locator = new Locator(this.config.geoCoder);
    locator.outSpatialReference = sRef;
    var dadds = []; 
    array.map(depotLocs, function(depotLoc, index){
      dadds.push({
        "OBJECTID":index,
        "SingleLine":depotLoc
      });
    };
    var options = { 
      addresses: dadds, 
      outFields: ["Loc_name"] 
    };
    locator.addressesToLocations(options, function(result){
        array.map(result.locations, function(candidate){
          facilitiesGraphicsLayer.add(new Graphic(new Point(candidate.location.x, candidate.location.y, sRef))); 
          depotGraphic.push(new Graphic(new Point(candidate.location.x, candidate.location.y, sRef)));
        } 
      }); 
    }, this);
0 Kudos
HamishKingsbury1
Occasional Contributor

Cheers Robert, I have tried something similar, but I will try your approach.

And well done on winning the competition! With all the help you've given me recently I think you very much deserve it!

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Thanks Hamish

0 Kudos
HamishKingsbury1
Occasional Contributor

So using your code I'm getting the same issue when I tried to implement the addressesToLocations.. The console is saying its a 400 error

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Hamish,

   Whose geocoding service are you using, one of your own or esris? If you are using esris then batch geocoding costs credits against your AGOL account.

0 Kudos
HamishKingsbury1
Occasional Contributor

We're using our own. We've access to a few and I've pointed it to them as well with no luck

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Hamish,

   So it has to be something in the overall code then. Can you share more of the code with me?

0 Kudos