Hey there
I've a list of addresses that I want to solve, ive tried two things:
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
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.
Unfortunately that's still returning them out of order... Its really bizarre.
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);
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!
Thanks Hamish
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
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.
We're using our own. We've access to a few and I've pointed it to them as well with no luck
Hamish,
So it has to be something in the overall code then. Can you share more of the code with me?