2

I'm using the function below to successfully add data to a popup in Leaflet.

Unfortunately, only the data from the last feature found is added. Is there any way to add the data from all features found to the popup.

function identify (e) {

var BBOX = map.getBounds().toBBoxString();
var WIDTH = map.getSize().x;
var HEIGHT = map.getSize().y;
var X = map.layerPointToContainerPoint(e.layerPoint).x;
var Y = map.layerPointToContainerPoint(e.layerPoint).y;
var url = 'http://maps.dartmoor.gov.uk/geoserver/wms/?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetFeatureInfo&layers=general:dnpa_planapps&BBOX='+BBOX+'&FEATURE_COUNT=5&info_format=application/json&propertyName=appcode,url&HEIGHT='+HEIGHT+'&WIDTH='+WIDTH+'&query_layers=general:dnpa_planapps&SRS=EPSG:4326&buffer=15&X='+X+'&Y='+Y;  

$.ajax({
    url:url,
    datatype: "json",
    type: "GET",
    success:handleJson
    }); 
    function handleJson(data) {
    for (var i = 0; i < data.features.length; i++) {
    var feature = data.features[i];
    console.log(feature)
        L.popup()
        .setLatLng(e.latlng)
        .setContent(L.Util.template("<h2>{appcode}</h2><p>{url}</p>", feature.properties))
        .openOn(map);
        }
    }
}

1 Answer 1

1

I think your loop needs to be something like:

var txt="";
for (var i = 0; i < data.features.length; i++) {
    var feature = data.features[i];
    console.log(feature);
    txt = txt.concat( L.Util.template("<h2>{appcode}</h2><p>{url}</p>", feature.properties));     
 }
 L.popup()
   .setLatLng(e.latlng)
   .setContent(txt)
   .openOn(map);   
2
  • Thanks but that still doesn't work for me. If I click on the map the console logs each feature but only adds one to the popup?
    – Matt T
    Sep 8, 2016 at 10:13
  • 1
    I forgot to declare txt before the loop. Once this was done it worked. Do you want to edit your answer to include that or is it obvious?
    – Matt T
    Sep 8, 2016 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.