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

Load data on layer show #8

Closed
ebertti opened this issue Sep 3, 2015 · 7 comments
Closed

Load data on layer show #8

ebertti opened this issue Sep 3, 2015 · 7 comments

Comments

@ebertti
Copy link

ebertti commented Sep 3, 2015

Have some way to only load data (AJAX) after clicking on a layer button?

@stefanocudini
Copy link
Owner

???

@ebertti
Copy link
Author

ebertti commented Apr 9, 2016

Hey man, wait a litle more then a hour to close this issue, I'm waiting for half year. 😃

Let my explain:

To declare a over layer, you need to do something like this:

var overLayers = [
    {
        name: "Drinking Water",
        icon: '<i class="icon icon-water"></i>',
        layer: L.geoJson(WaterGeoJSON)
    },   
];

But the necessary data must already be loaded at the time of the overlayer statement

My suggestion is make the layer load using a data loaded or a callback function, only when the layer is selected to be shown:

var overLayers = [
    {
        name: "Drinking Water",
        icon: '<i class="icon icon-water"></i>',
        layer: function(layer){
            $.get('some/url/path').done(function(data){
                layer.render(L.geoJson(data))
            });
        }
    },   
];

This will be helpful when you need to show to much data on a layer.

@stefanocudini stefanocudini reopened this Apr 9, 2016
@stefanocudini
Copy link
Owner

Closing the issue was an incentive to describe best your idea :-)

Great idea!

@stefanocudini
Copy link
Owner

stefanocudini commented Apr 19, 2016

Hi @ebertti in latest days I have thought a lot about this solution..
But I implemented new way most generic.. for adding layers at runtime, look here:
http://labs.easyblog.it/maps/leaflet-panel-layers/examples/dynamic-layers.html

From version 0.2.2 you can add new layer using two methods:
panel.addBaseLayer(item);
panel.addOverlay(item);

Your code can be changed in this way:

var panel = L.control.panelLayers();

$.getJSON('some/url/path.geojson', function(data){
    panel.addOverlay({
        name: "Drinking Water",
        icon: '<i class="icon icon-water"></i>',
        layer: L.geoJson(data)
    });
});

@ebertti
Copy link
Author

ebertti commented Apr 19, 2016

Hi @stefanocudini, 👍 this solution already help.

But lets think this scenario. If I have 10 overlay with 200 points. With this solution I steel need to preload all data before add the overlay on map interface.

@stefanocudini
Copy link
Owner

stefanocudini commented Apr 19, 2016

waiting upgrade compatibility to Leaflet 1.0, this is the best solution:

var panel = L.control.panelLayers(),
var river = L.geoJson();

map.on('layeradd', function(e) {
    if(L.stamp(e.layer) === L.stamp(river)) {
        $.getJSON('data/river.json', function(data) {
            river.addData( data );
        });
    }
});

panel.addOverlay({
    name: "Drinking Water",
    icon: '<i class="icon icon-water"></i>',
    layer: river
});

add an empty layer and after addData on this

@ebertti
Copy link
Author

ebertti commented Apr 20, 2016

#16

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