The easiest way is to check for the _map property in 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 when using the _map property, keep in mind that the _ property prefix implies that this is a private property that you usually should not use. It could be changed or deleted in future versions of the Flyer. You will not come across this if you use the following approach:
Attaching the link of your custom control to the 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 the link to your map instance as follows:
if (map.customControl) { ... }
Or create a method and include it in L.Map :
L.Map.include({ hasCustomControl: function () { return (this.customControl) ? true : false; } });
This will work as follows:
var customControl = new L.Control.Custom(); map.addControl(customControl); map.hasCustomControl();
Here is a demonstration of the concept on Plunker: http://plnkr.co/edit/nH8pZzkB1TzuTk1rnrF0?p=preview
iH8
source share