[GIS] OpenLayers Vector Layer callback does not execute with GeoJSON

openlayers-2

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…

Best Answer

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.