0

I'm adding a layer with a source requested by ol.source.GeoJSON on moveend. How can I abort pending ajax requests if the map is panned again before the request finishes?

map.on('moveend', function(){
    map.removeLayer(highlightedLayer);
    var theSource = new ol.source.GeoJSON({
            url: 'wfs.php?bbox='+bbox
            });
    var highlightedSource = new ol.source.GeoJSON({});
    theSource.on('change', function(e){
            if(theSource.getState() == 'ready'){
                    var features = theSource.getFeatures();
                    $.each(features, function(k,v){
                            if(v.n.filter == 'include'){
                                    highlightedSource.addFeature(features[k]);
                                    }
                            });
                    highlightedLayer.setSource(highlightedSource);
                    map.addLayer(highlightedLayer);
                    }
            });
    });
mms
  • 5
  • 2

1 Answers1

0

Instead of adding the layer directly at the moveend you could fetch the GeoJson with ajax (which can be cancelled) and when complated add geojson data using the object paramter instead of the url.

Heres the docs for GeoJSON source: http://openlayers.org/en/v3.4.0/apidoc/ol.source.GeoJSON.html?unstable=true

Heres how you cancel a ajax request: Abort Ajax requests using jQuery

Hope this helps you.

Community
  • 1
  • 1
Poul K. Sørensen
  • 16,950
  • 21
  • 126
  • 283