pulling data out of map click .run

4050
5
Jump to solution
02-26-2015 12:25 PM
TimLohnes
New Contributor II

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

0 Kudos
1 Solution

Accepted Solutions
OwenEarley
Occasional Contributor III

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.

View solution in original post

0 Kudos
5 Replies
OwenEarley
Occasional Contributor III

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;
          });
}   
0 Kudos
OwenEarley
Occasional Contributor III

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;
            });
}  
0 Kudos
TimLohnes
New Contributor II

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

  1.            .run(function (error, featureCollection, response) { 
  2.                 console.log("UTC Offset: " + featureCollection.features[0].properties.APN_PQ); 
  3.                 return featureCollection.features; 
  4.             }); 
  5.            return clickedParcels;
  6. }   

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.

0 Kudos
OwenEarley
Occasional Contributor III

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.

0 Kudos
TimLohnes
New Contributor II

Thanks, Owen. I believe that to be the problem. We'll see if I can make it through promises unscathed....

0 Kudos