0

I am building a leaflet map with feature layers that are hosted on ArcGIS online.

I would like to write a simple function so that when I click on a feature, the value of a particular column in the attribute table will be saved to a variable and logged to the console.

I have been able to get the value to appear in a pop-up, but not log to the console.

I have a feeling the solution has something to do with this, but I have limited experience with esri-leaflet so there is something I am missing:

https://esri.github.io/esri-leaflet/api-reference/tasks/identify-features.html

  var map = L.map('map').setView([40.1562,-75.057104], 9);
  L.esri.basemapLayer("Gray").addTo(map);

  var trails = L.esri.featureLayer({
    url: "https://services1.arcgis.com/Ps1YVQiv5JQLIFu2/arcgis/rest/services/CIRCUIT_TRAILS/FeatureServer/0",
    style: function () {
      return { color: "#70ca49", weight: 2 }; 
    }
  }).addTo(map);

  var popupTemplate = "<h3>{CIRCUIT_ID}</h3>";

  trails.bindPopup(function(e){
    return L.Util.template(popupTemplate, e.feature.properties)
    console.log(e.feature.properties.CIRCUIT_ID)

  });
  • 1
    "I have been able to get the value to appear in a pop-up, but not log to the console." Can you post that code? – Pat Dec 06 '17 at 20:53
  • Pat, I just added the code I have so far. I can also upload the HTML if that would help. – global_stats Dec 06 '17 at 21:01
  • What does `console.log(e.feature.properties)` (not including `.CIRCUIT_ID`) return? – Pat Dec 06 '17 at 21:03
  • It doesn't return anything. I should clarify that in the final product, I don't actually want a pop-up to appear. I only want it to pass the value to a variable. Now I am experimenting with this: map.on('click', function(n) { console.log(n.feature.properties.CIRCUIT_ID) }) – global_stats Dec 06 '17 at 21:10
  • You're `console.log` is coming after the return. Unless the stuff after the return statement is hoisted its not going to be run. Move it to above the return statement and see if its returning anything. – Pat Dec 06 '17 at 21:18
  • Great - that did allow me to log the value to the console. However, I don't want a pop-up to appear in the final version. If I remove " return L.Util.template(popupTemplate, e.feature.properties) " then I get an arror in the console. That is making me think I can't rely on bindPopup – global_stats Dec 06 '17 at 21:41
  • Remove the `.bindPopup` and attach a click event instead that logs the targets attributes. http://jsfiddle.net/vbowk9rw/ is an example of what I mean – Pat Dec 06 '17 at 22:10
  • @pat's code sample is in entirely the wrong API, but he's still on to something. since esri leaflet featureLayer inherits from leaflet's own Layer, it *does* emit a click event that you can listen for. – john gravois Dec 07 '17 at 04:47

0 Answers0