Tuesday, 6 February 2018

openlayers 2 - Filter Vector Layer Features


I'm using OpenLayers - 2.11,GeoServer 2.13, ExtJS 3.4, and Java for developing GIS based web applications.


I have added some point features in one vector layer. It has id, name, address, etc.


Can I filter some points with 'name' from that vector layer and only show matched features in vector layer? I have to filter some points and later I have to show all hidden data.


eg: enter image description here




Answer



Since you already setup the stylemaps for diplaying labels. You can easily do this by checking the feature attribute value of the required field with the search text


Try the below code to search in attributes


//JS code
function search() {
var search_name = document.getElementById("search_text").value;
var features = layer.features;
layer.destroyFeatures();
for(var i=0; i< features.length; i++) {


//features[i].attributes.name. you have the attribute field "name"

if(features[i].attributes.name === search_name) {
layer.addFeatures([features[i]])
}
}

layer.redraw();
}






Update: hide/show using renderIntent


    //JS code

function search() {
var search_name = document.getElementById("search_text").value;
var features = layer.features;

for(var i=0; i< features.length; i++) {

//features[i].attributes.name. you have the attribute field "name"

//hide a feature by default
features[i].renderIntent = "delete";
if(features[i].attributes.name === search_name) {
//show a feature value of name attribute is search_name
features[i].renderIntent = "default"
}

}

layer.redraw();
}

The second method becomes faster as renderIntent="delete" will set the css property display: none;(hiding) and renderIntent="default" will use the default rendering from your stylemap obivously showing the features.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...