1

I am not really a programmer and I'm trying to create a webservice. I copy and pasted some script examples which made my script what it is now. I have multiple wms layers, but want to show different info in the infowindow for each wms. So I thought this can be done by an if-then-else statement. First, I created the getfeatureinfo variable including the layers which need to have information shown.

var control_identify = new OpenLayers.Control.WMSGetFeatureInfo({
    title : 'Klik op een lijn voor informatie',
    layers : [ AmiceHerkomst, AmiceVerwerking ],
    queryVisible : true,
    infoFormat : 'application/vnd.ogc.gml',
    vendorParams: {
        buffer: 5  // geoserver buffer in pixels
    },
    eventListeners : {
        getfeatureinfo : function(event){
            showInfo(event);
        }
    }
});

control_identify.events.register("getfeatureinfo", this, showInfo);

then I create the showinfo function. Here I want to make an if-then-else for each wms layer I have. Something in the line of if(evt.features.length != 0) and layer = AmiceHerkomst { followed by the attributes I want from that layer. For the next layer I add at the end else if (evt.features.length != 0) and layer = AmiceVerwerking { with the attributes I want for this layer and so on. Scripting like I said above will give an error, but I think something like this should be possible?

function showInfo(evt) {
    var f = 0;
    var temstr = "";
    if(evt.features.length != 0){
        for (f = 0; f <= evt.features.length - 1; f++) {

            temstr = temstr + "<table>";
            for ( var key in evt.features[f].attributes) {
                temstr += "<tr>";

                switch (key){
                case 'afvalstroo':
                    temstr += "<td width= 71px>Afvalstroom</td><td>"
                    + evt.features[f].attributes[key] + "</td>";
                    break;
                case 'plaatsloca':
                    temstr += "<td width= 71px>Plaatsnaam</td><td>"
                    + evt.features[f].attributes[key] + "</td>";
                    break;

                default:
                };
                temstr += "</tr>";
            }
            temstr = temstr + "</table>";
        };
        log("<b>Informatie</b><br/>" + temstr, true);
    } else {
        log("<b>Informatie</b><br/>" + "Niets gevonden op deze locatie", true);
    }
}
3

You can switch to response format JSON and iterate through the response-features:

info = new OpenLayers.Control.WMSGetFeatureInfo({
            url: 'http://demo.opengeo.org/geoserver/wms', 
            title: 'Identify features by clicking',
            queryVisible: true,
             infoFormat : 'application/json',
             drillDown: true,
            eventListeners: {
                getfeatureinfo: function(event) {

                    popuphtml="";

                    retrieved_response=JSON.parse(event.text);
                    retrieved_objects=retrieved_response.features;
                    if(retrieved_objects.length>0)
                    {


                    for(var i=0;i<retrieved_objects.length;i++)
                    {
                    loop_layer_id=retrieved_objects[i].id;
                    loop_layer_id = loop_layer_id.substring(0,loop_layer_id.indexOf("."));
                    //console.log(loop_layer_id);
                    if(loop_layer_id=="tasmania_state_boundaries")
                    {
                    popuphtml=popuphtml+"<br />"+loop_layer_id+"<br />STATE: "+retrieved_objects[i].properties.STATE+"<br />";
                    }
                    else if(loop_layer_id=="tasmania_roads")
                    {
                    popuphtml=popuphtml+"<br />"+loop_layer_id+"<br />TYPE: "+retrieved_objects[i].properties.TYPE+"<br />";
                    }
                    else if(loop_layer_id=="tasmania_water_bodies")
                    {
                    popuphtml=popuphtml+"<br />"+loop_layer_id+"<br />WaterTYPE: "+retrieved_objects[i].properties.WATER_TYPE+"<br />";
                    }
                    else
                    {
                    popuphtml=popuphtml+"<br />other content"+"<br />";
                    }

                    }
                    //console.log(popuphtml);
                    map.addPopup(new OpenLayers.Popup.FramedCloud(
                        "chicken", 
                        map.getLonLatFromPixel(event.xy),
                        null,
                       popuphtml 
                        ,
                        null,
                        true
                    ));

                    }
                    else
                    {
                    alert("no response...");
                    }



                }
            }
        });
        map.addControl(info);
        info.activate();

enter image description here

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.