Hi all,
Just playing around with esri-leaflet to see if I can get everything done easier.
I a bit of a novice, but was able to quickly access my data attributes on map click with Tasks.identifyFeatures.
However I don't know how to get that data out of that task and into a variable so I can process the selection elsewhere.
In the console.log in run, I can get all the info I want. How do I pass it back to the parcels variable in map.on and then get access to the data?
map.on('click', function (e) {
parcels = queryOnMapClick(e);
});
function queryOnMapClick(clicked) {
var clickedParcels = L.esri.Tasks.identifyFeatures(pathtoservice)
.on(map)
.at(clicked.latlng)
.layers('visible:4')
.run(function (error, featureCollection, response) {
console.log("UTC Offset: " + featureCollection.features[0].properties.APN_PQ);
});
}
Thanks for any tips.
-Tim
Solved! Go to Solution.
The run function is an asynchronous task - you may be attempting to use the parcels variable before the run function has assigned anything to it.
I haven't tried leaflet yet but you should look into promises.
You could assign the variable in your run function:
map.on('click', function (e) {
queryOnMapClick(e);
});
function queryOnMapClick(clicked) {
var clickedParcels = L.esri.Tasks.identifyFeatures(pathtoservice)
.on(map)
.at(clicked.latlng)
.layers('visible:4')
.run(function (error, featureCollection, response) {
console.log("UTC Offset: " + featureCollection.features[0].properties.APN_PQ);
parcels = featureCollection.features;
});
}
Alternatively, return a value from within the run function:
map.on('click', function (e) {
parcels = queryOnMapClick(e);
});
function queryOnMapClick(clicked) {
var clickedParcels = L.esri.Tasks.identifyFeatures(pathtoservice)
.on(map)
.at(clicked.latlng)
.layers('visible:4')
.run(function (error, featureCollection, response) {
console.log("UTC Offset: " + featureCollection.features[0].properties.APN_PQ);
return featureCollection.features;
});
}
Thanks, Owen. I can't get access to the data anywhere outside of .run.
With your examples most of the time it would just say undefined.
if I return the main variable 'clickedParcels' at the bottom like
In the console I get some response from console.log(parcels):
XMLHttpRequest {statusText: "", status: 0, responseURL: "", response: "", responseType: ""…}
But I haven't been able to figure out how to retrieve the data out of that. I'm just trying to get a set of data that I can plug into other functions.
The run function is an asynchronous task - you may be attempting to use the parcels variable before the run function has assigned anything to it.
I haven't tried leaflet yet but you should look into promises.
Thanks, Owen. I believe that to be the problem. We'll see if I can make it through promises unscathed....