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

2 Answers 2


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.


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)**

  • 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, 2012 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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