Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with data:loaded #45

Open
rw-donaldp opened this issue Oct 28, 2016 · 3 comments
Open

Problems with data:loaded #45

rw-donaldp opened this issue Oct 28, 2016 · 3 comments

Comments

@rw-donaldp
Copy link

rw-donaldp commented Oct 28, 2016

Hi I'm using the plug-in to load GeoJSON data from a Drupal server into a leaflet map. The problem I appear to be having is with the on data:loaded event triggering before all of the data seems to have been made available.
The following is "almost" working but rather than the 200+ markers that should be present only the first 75 seem to be present when the data:loaded function is called. These 75 markers display correctly on the map in appropriate clusters.

var currentMarkers = new L.MarkerClusterGroup(cluster_options).addTo(map);

     var allSchemes =  new L.GeoJSON.AJAX("//"+domain+"/schemes/map/layer/all-schemes");`

     allSchemes.on('data:loaded', function () {
        currentMarkers.addLayer(this);
     });

If, using console.log() I output the number of layers in the allSchemes object or list the keys of the allSchemes._layers object the answer is always 75. However, if I log the object allSchemes._layer itself, then the object contains all 200+ entries I would expect. I presume that the console only populates the full _layers object to display the list of contents only when it is clicked on.

Looking at the keys given to each layer object in allSchemes._layers, there appears to be gap in the sequence after the first 75 before the 76th. (Often a gap of about 50 numbers). The remainder of the keys again run in sequence.

To confuse things further or maybe clarify things?, the map has a user interaction that allows you to filter the markers using code which effectively does the following. Here all of the 200+ markers are present and can be displayed correctly.

      currentMarkers.clearLayers();
      // Go through each marker to determine if it should be included.
      allSchemes.eachLayer(function(layer) {
          if (includeLayer(layer)) {
            currentMarkers.addLayer(layer);
          }
      });

I can only presume that when the data:loaded event is triggered all the data has not been loaded or is available in the object.

Am I doing something wrong or is there anything I could do to correct this behaviour?

Thanks.

@calvinmetcalf
Copy link
Owner

this downloads and adds all the layers before calling that event, try adding allSchemes to the map directly to see if that works...

@rw-donaldp
Copy link
Author

Adding allSchemes directly to the map appears to add all the markers, but so the following also works:

    allSchemes.on('data:loaded', function () {
        setTimeout(deferred_add,200);
    });

    function deferred_add() {
        currentMarkers.addLayer(allSchemes);
    }

If you log the number of _layers of allSchemes before the delay it returns 75 and after the delay it says 263 which would imply that the 'data:loaded' is occurring before allSchemes is fully prepared. Additionally if the delay is reduced it gets to a point where the delay is not enough for this to occur. I'm reluctant to use this technique as a "fix" as this delay could well be network dependant. I will do some further tests when I get time.

@calvinmetcalf
Copy link
Owner

it's not going to be network dependent, the data is added to the layer once, most likely there is a delay in leaflet adding all of the layers somehow, what you can do is you can use the ajax loader directly

L.Util.ajax(url).then(function (data){
   // add layers here
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants