1

I have a vector layer in openlayers with source in GEOJSON (custom php server, which I have in complete control) and BBOX strategy.

Vector points contain point sources which have different attributes. I want to implement some filters by one or two attributes. I can manage server side php code - for e.g. I can get the filters by adding URL parameters to existing url like:

Existing URL: mywebservice.php
Filtered URL: mywebservice.php?someattributte=somevalue

How do I implement it on the OpenLayers side?

My code is as follows:

            var in_options = {
                'internalProjection': EPSG_900913,
                'externalProjection': EPSG_4326
            };
            var ems_sources = new OpenLayers.Layer.Vector(EMS_SOURCES_TITLE, {
                strategies: [
                             new OpenLayers.Strategy.BBOX({
                                 resFactor: 2,
                                 active: true,
                                 autoActivate: true,
                                 ratio: 1.3
                             })

                         ],
                 protocol: new OpenLayers.Protocol.HTTP({
                     url: EMS_SOURCES_WS,
                     callbackKey: 'callback',
                     format: new OpenLayers.Format.GeoJSON(in_options)
                 })
            }); 
2

You can pass params object in the OpenLayers.Protocol.HTTP. See here http://dev.openlayers.org/docs/files/OpenLayers/Protocol/HTTP-js.html#OpenLayers.Protocol.HTTP.params

your protocol part of code should look like

protocol: new OpenLayers.Protocol.HTTP({
                 url: EMS_SOURCES_WS,
                 callbackKey: 'callback',
                 format: new OpenLayers.Format.GeoJSON(in_options),
                 params: {someattributte:somevalue}
             })
1

I am answering my own question based on retrieved answers just to make it clearer.

           //Declare a variable layer ems_sources
           var in_options = {
                'internalProjection': EPSG_900913,
                'externalProjection': EPSG_4326
            };
            var ems_sources = new OpenLayers.Layer.Vector(EMS_SOURCES_TITLE, {
                strategies: [
                             new OpenLayers.Strategy.BBOX({
                                 resFactor: 2,
                                 active: true,
                                 autoActivate: true,
                                 ratio: 1.3
                             })

                         ],
                 protocol: new OpenLayers.Protocol.HTTP({
                     url: "myWebServiceURL.php",
                     callbackKey: 'callback',
                     format: new OpenLayers.Format.GeoJSON(in_options),
                     params: {filter: ""} //initial filter is set to empty string
                 })
            });


            //add layer to the map
            map.addLayer(ems_sources);



           //call this function in order to refresh data and apply filter
           function refreshData(filter) {
                 ems_sources.protocol.params.filter= "myAttributeValue";
           }

By calling refreshData function and providing filter attribute the URL for sources retrieval will look like this:

myWebServiceURL.php?filter=myAttributeValue

I should just apply the server side web service to filter the results according to the filter parameter.

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.