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

New method setInteractive(interactive) for interactive layers #5442

Open
perliedman opened this issue Apr 7, 2017 · 8 comments · May be fixed by #9055
Open

New method setInteractive(interactive) for interactive layers #5442

perliedman opened this issue Apr 7, 2017 · 8 comments · May be fixed by #9055

Comments

@perliedman
Copy link
Member

For interactive layers (layers with the interactive option, or extending the virtual InteractiveLayer class), we could provide a setInteractive method to change the state after the layer is created.

As it is now, there's no way to change the interactive state after a layer has been created, causing issues like #5441.

@maxdeepfield
Copy link

any plans about this feature development?

@perliedman
Copy link
Member Author

@maxdeepfield no plans, as far as I know.

I'd be happy to guide if someone would be willing to take this on.

@Ulrikop
Copy link

Ulrikop commented Jul 22, 2018

I would like to see a method like that. It feels false to re-create a marker/line just because I want to change the behavior of the interactivity

@verbeeckjan
Copy link

I really need this functionality too, any updates or workarounds @perliedman?

@Jadaw1n
Copy link

Jadaw1n commented Sep 24, 2018

Made this little function as a "plugin":

L.Layer.prototype.setInteractive = function (interactive) {
    if (this.getLayers) {
        this.getLayers().forEach(layer => {
            layer.setInteractive(interactive);
        });
        return;
    }
    if (!this._path) {
        return;
    }

    this.options.interactive = interactive;

    if (interactive) {
        L.DomUtil.addClass(this._path, 'leaflet-interactive');
    } else {
        L.DomUtil.removeClass(this._path, 'leaflet-interactive');
    }
};

Just call it like every other function e.g. layer.setInteractive(true) or layerGroup.setInteractive(false).

@jmayer86
Copy link

jmayer86 commented Jan 28, 2019

Thanks @Jadaw1n ! That worked like a charm, here it is in Typescript for anyone who wants it:

export class InteractiveImageOverlay extends L.ImageOverlay {
    setInteractive(interactive: boolean) {
        this.options.interactive = interactive;

        if (interactive) {
            L.DomUtil.addClass(this.getElement(), 'leaflet-interactive');
            this._map.on('click', (event: L.LeafletMouseEvent) => {
                this.fireEvent('click', event);
            });
        } else {
            L.DomUtil.removeClass(this.getElement(), 'leaflet-interactive');
            this._map.removeEventListener('click');
        }
    }
}

matafokka added a commit to matafokka/SynthFlight that referenced this issue Mar 24, 2021
… markers) when ALS layer is not being selected.

Added setInteractive(), getInteractive() and isInteractive() methods to L.Layer (thanks to Piero "Jadaw1n" Steinger; comment: Leaflet/Leaflet#5442 (comment); home page: https://github.com/Jadaw1n)
@jktu2870
Copy link

jktu2870 commented Jul 8, 2021

@Jadaw1n it is a nice solution you've shared, unfortunately for me it doesn't work with canvas elements

@Leaflet Leaflet deleted a comment from Falke-Design Nov 9, 2021
@Malvoz Malvoz changed the title Feature: new method setInteractive(interactive) for interactive layers New method setInteractive(interactive) for interactive layers Nov 4, 2022
someonewithpc added a commit to someonewithpc/Leaflet that referenced this issue Aug 2, 2023
someonewithpc added a commit to someonewithpc/Leaflet that referenced this issue Aug 2, 2023
someonewithpc added a commit to someonewithpc/Leaflet that referenced this issue Aug 2, 2023
someonewithpc added a commit to someonewithpc/Leaflet that referenced this issue Aug 3, 2023
someonewithpc added a commit to someonewithpc/Leaflet that referenced this issue Aug 3, 2023
@someonewithpc someonewithpc linked a pull request Aug 3, 2023 that will close this issue
someonewithpc added a commit to someonewithpc/Leaflet that referenced this issue Sep 8, 2023
someonewithpc added a commit to someonewithpc/Leaflet that referenced this issue Sep 8, 2023
@DrPitLazarus
Copy link

@Jadaw1n Wanted to say thanks for your code. I ran into this issue changing my rendering from canvas to SVG. Surprised this isn't in leaflet by now.

someonewithpc added a commit to someonewithpc/Leaflet that referenced this issue Jan 11, 2024
someonewithpc added a commit to someonewithpc/Leaflet that referenced this issue Jan 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants