Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out if a leaflet control has already been added to the map

Tags:

leaflet

I wrote a custom Leaflet control. It's some kind of legend that may be added for each layer. The control itself has a close button to remove it from the map (like a popup). The control can be added by clicking a button. My problem is that the user may add the same control to the map several times. So what I need is to test if this specific control has already been added to the map and, if so, don't add it again.

I create a control for each layer, passing some options

var control = L.control.customControl(mylayer);

and add it to my map on button click

control.addTo(map);

Now imagine the control has a close button and may be closed. Now if the user clicks the button again, I only want to add the control if it's not already on the map - something like this (hasControl is pseudocode, there is afaik no such function)

if(!(map.hasControl(control))) {
    control.addTo(map);
}

For simplicity I made an example where I create a zoom control and add it twice here.

like image 269
Krxldfx Avatar asked Oct 15 '15 11:10

Krxldfx


People also ask

How do you add a layer to a map in Leaflet?

Step 1 − Create a Map object by passing a <div> element (String or object) and map options (optional). Step 2 − Create a Layer object by passing the URL of the desired tile. Step 3 − Add the layer object to the map using the addLayer() method of the Map class.

What is Leaflet control?

In Leaflet, a “layer” is anything that moves with the map. In contraposition to that, a “control” is a HTML element that remains static relative to the map container, and a “handler” is a piece of invisible code that changes the map's behaviour.

How do you add a Leaflet to a plugin?

You can attach the Leaflet plugin to your map with the help of Github or any other repository via adding the additional . js and . css file into the leaflet html file in the header section. The leaflet plugins really improves the performance and usability of Leaflet Js.


1 Answers

Easiest way is to check for the existence of the _map property on your control instance:

var customControl = new L.Control.Custom();

console.log(customControl._map); // undefined

map.addControl(customControl);

console.log(customControl._map); // returns map instance

But please keep in mind, when using the _map property, that the _ prefix of the property implies that it's a private property, which you are normally not supposed to use. It could be changed or removed in future versions of Leaflet. You're not going to encounter that if you use the follow approach:

Attaching a reference of your custom control to your L.Map instance:

L.Control.Custom = L.Control.extend({
    options: {
        position: 'bottomleft'
    },
    onAdd: function (map) {
        // Add reference to map
        map.customControl = this;
        return L.DomUtil.create('div', 'my-custom-control');
    },
    onRemove: function (map) {
        // Remove reference from map
        delete map.customControl;
    }
});

Now you can check for the reference on your map instance like so:

if (map.customControl) { ... }

Or create a method and include it in L.Map:

L.Map.include({
    hasCustomControl: function () {
        return (this.customControl) ? true : false;
    }
});

That would work like this:

var customControl = new L.Control.Custom();

map.addControl(customControl);

map.hasCustomControl(); // returns true

map.removeControl(customControl);

map.hasCustomControl(); // returns false

Here's a demo of the concept on Plunker: http://plnkr.co/edit/nH8pZzkB1TzuTk1rnrF0?p=preview

like image 78
iH8 Avatar answered Nov 02 '22 23:11

iH8