Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

Can you please help me, how to 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)
                 })
            }); 

Thank you very much for your answer.

Matej

share|improve this question

2 Answers 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}
             })
share|improve this answer
    
Then I have to refresh params by calling: layer.protocol.params.someattribute= "someothervalue"; It works then. –  Matej Aug 29 '13 at 13:43
    
i'm not sure, i understand. You can edit the above answer to mae it clearer. It would be great help to others as well. –  neogeomat Aug 29 '13 at 14:30
up vote 1 down vote accepted

I 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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