TransWikia.com

Access individual feature properties when running l.esri.find or l.esri.query

Geographic Information Systems Asked by brandon p on December 2, 2020

Here is what my site looks like: https://brandonlprice.github.io/landrecords/. How can I access the feature properties for a layer when running a find or a query on a layer? I have the the feature.properties.length being displayed in a .innerhtml to show the total count of features returned. I also would like to be able to display the individual attributes for each feature in a layer in a .innerhtml when they are clicked on in the map. The onEachFeature would be one way although I do not know how to link that to the query.run or find.run. How can I do that? Are there any other way to access the individual attributes. My query snippet are below.

Query:

function tmqueryFunction() {
     latlng = markerLayer.getLatLng();
     tmbufferResult.clearLayers();
document.getElementById("tractcounts").innerHTML = '';
    if (document.getElementById("tm1").checked == true){
       tmquery.nearby(latlng, distance);
       tmquery.run(function (error, featureCollection, response) {
  if (error) {
    console.log(error);
    return;
  }
  tmbufferResult.addData(featureCollection).addTo(map)
  document.getElementById('tractcounts').innerHTML = 'Tract Map Records: ' + 
  featureCollection.features.length;      
  });  
  map.fitBounds(L.geoJSON(featureCollection).getBounds());
});
}
}

One Answer

You can access feature properties by iterating through layers/features with .eachLayer method. Feature properties are accessible through layer feature property.

In your case, if you would want to attach popup with some of the feature properties to each layer/feature, your code could look something like this:

function tmqueryFunction() {
  if (!markerLayer) return;
  
  latlng = markerLayer.getLatLng();
  tmbufferResult.clearLayers();
  document.getElementById("tractcounts").innerHTML = '';

  if (!document.getElementById("tm1").checked) return;

  tmquery.nearby(latlng, distance);
  tmquery.run(function (error, featureCollection, response) {
    if (error) {
      console.log(error);
      return;
    }
    tmbufferResult.addData(featureCollection).addTo(map)
    document.getElementById('tractcounts').innerHTML = 'Tract Map Records: ' + featureCollection.features.length;  
    tmbufferResult.eachLayer(function(layer) {
      layer.bindPopup(
        'REFERENCE: ' + layer.feature.properties.REFERENCE + '<br>' +
        'SUB_NAME: ' + layer.feature.properties.SUB_NAME);
    });

    map.fitBounds(L.geoJSON(featureCollection).getBounds());
  });
}

Here is an example of such popup on your map:

enter image description here

Answered by TomazicM on December 2, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP