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.

Why does this callback get ignored:

new OpenLayers.Layer.Vector("My layer",
      {
        projection : "EPSG:4326",
        strategies : [ new OpenLayers.Strategy.Fixed(), refreshStrategy ],

        protocol : new OpenLayers.Protocol.HTTP(
        {            
          url    : "myphpurl.php",
          params : { rand : Math.random() * 1000 },
          format : new OpenLayers.Format.GeoJSON(),
          callback: function(){alert('test');}
        }),

I'm trying to get access to how many features were returned...

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Callback get ignored because it's possible is overridden by another callback function internally (e.g. OpenLayers.Strategy.BBOX.merge). Use events mechanism for get feature count:

layer = new OpenLayers.Layer.Vector("My layer", ...)
layer.events.on({"loadend": function(e){console.log(e.object.features.length)}})

According to documentation callback property of OpenLayers.Protocol.HTTP is not marked as APIProperty, so it is for internal use only.

share|improve this answer

I think you're just missing the part where you ask the layer to read the data (see ** line below):

    var request = OpenLayers.Request.GET({
        url: serviceUrl.url,
        callback: function(request) {
            var geojson_format = new OpenLayers.Format.GeoJSON({
                'internalProjection': toProj,
                'externalProjection': fromProj
            });
            **gfs = geojson_format.read(request.responseText)**
            lyr.addFeatures(gfs);
            setExtentToData();

        }
    });
share|improve this answer
    
Thanks @yalestar! It looks like the request was already being made, but the callback was being overridden. I eventually just ended up overriding handleResponse to get that the info I needed. –  Jonathan Dec 13 '12 at 14:39

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.