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 problem creating feature count window. I have urls stored in as json object. I iterate it with jquery and send request to remote data. The I have featureStore which is initially set to 0. When the request is done i try to update it with new feature count but it only updates the last url of array here "S". The console of handler has always last value of the array here "S". Is there any way I can synchronize this? I think there are mulitple request and some request returns value faster than other without considering which url request is send. How do I synchronize so that I can update featureStore eaxctly as the elements in URL.

                var current ;         
                var featuresStore = 
                                    {
                                     "F" : 0,
                                     "S" : 0
                                     };

                          var urls = 
                                        {
                                         "F" : "http://192.168.69.88:8080/geoserver/wfs?request=GetFeature&typename=Data:sites_2&version=1.1.1&resultType=hits&filter=(<Filter><PropertyIsEqualTo><PropertyName>Vendor</PropertyName><Literal>TE</Literal></PropertyIsEqualTo></Filter>)",
                                         "S" : "http://192.168.69.88:8080/geoserver/wfs?request=GetFeature&typename=Data:sites_3&version=1.1.1&resultType=hits&filter=(<Filter><PropertyIsEqualTo><PropertyName>Vendor</PropertyName><Literal>TE</Literal></PropertyIsEqualTo></Filter>)"   
                                        };

                  //Feature Store before 
                  console.log(featuresStore);

                   //reads nof of features  
                  var LegendHandler = function (request) {
                          var noOfFeature;
                             try {
                                    var xmlFormat  = new OpenLayers.Format.XML();
                                    var data  = xmlFormat.read(request.responseText);
                                    var format = new OpenLayers.Format.WFST.v1_1_0({});
                                    var result = format.read(data, {output: "object"});
                                    noOfFeature = result.numberOfFeatures;

                                }catch(e){
                                    $("#messageText").val("Error "+e);
                                    console.log("Error "+e);
                                    noOfFeature = 0;
                                }
                                   featuresStore.current = noOfFeature;
                                   console.log("The current value is "+current);
                            }
            //Iterating over each URLs          
             $.each(urls,function(i){
                                current = i;
                                console.log("The current value is "+current);
                        var request = OpenLayers.Request.GET({
                                    url: urls[i],
                                    callback: LegendHandler
                                });

                          });


                          //Feature Store after 
                          console.log(featuresStore);
share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

For example:

var arr = [];
LegendHandler = function(e){arr.push(e.status)};
OpenLayers.Request.GET({url: '***.json', callback:  LegendHandler});

It looks like you have edited your question. In this case for sending additional parameters to the callback function you should use scope option of OpenLayers.Request object. The solution of your task (I've tested it on my own data so you should to modify it a little bit):

var geojson = new OpenLayers.Format.GeoJSON();

function feature_count(request) {
    var features = geojson.read(request.responseText);
    fs[this.index].count = features.length;
}

var fs = [
    {
        'url': 'http://hostname/users.json',
        'feature_count': 0
    },
    {
        'url': 'http://hostname/users.json',
        'feature_count': 0
    }
];

for (var i=0; i<fs.length; i+=1) {
    OpenLayers.Request.GET({
        url: fs[i].url,
        success: feature_count,
        scope: {'index': i}
    });
}
share|improve this answer
    
i know i need some js lesson. can you please explain how to send parameter to legendhandler and what e is?? –  kinkajou Sep 28 '12 at 9:51
    
I've modified my answer, please check it out. –  drnextgis Oct 1 '12 at 3:49
    
@Kitex please accept an answer if it was helpful. –  drnextgis Oct 10 '12 at 1:43
    
thank u very much for help :) –  kinkajou Oct 10 '12 at 2:02
add comment

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.