Layer
export var Layer = Evented.extend({
options: {
pane: "overlayPane",
attribution: null,
bubblingMouseEvents: true,
},
addTo: function (map) {
map.addLayer(this);
return this;
},
remove: function () {
return this.removeFrom(this._map || this._mapToAdd);
},
removeFrom: function (obj) {
if (obj) {
obj.removeLayer(this);
}
return this;
},
getPane: function (name) {
return this._map.getPane(
name ? this.options[name] || name : this.options.pane
);
},
addInteractiveTarget: function (targetEl) {
this._map._targets[Util.stamp(targetEl)] = this;
return this;
},
removeInteractiveTarget: function (targetEl) {
delete this._map._targets[Util.stamp(targetEl)];
return this;
},
getAttribution: function () {
return this.options.attribution;
},
_layerAdd: function (e) {
var map = e.target;
if (!map.hasLayer(this)) {
return;
}
this._map = map;
this._zoomAnimated = map._zoomAnimated;
if (this.getEvents) {
var events = this.getEvents();
map.on(events, this);
this.once(
"remove",
function () {
map.off(events, this);
},
this
);
}
this.onAdd(map);
this.fire("add");
map.fire("layeradd", { layer: this });
},
});
Map.include({
addLayer: function (layer) {
if (!layer._layerAdd) {
throw new Error("The provided object is not a Layer.");
}
var id = Util.stamp(layer);
if (this._layers[id]) {
return this;
}
this._layers[id] = layer;
layer._mapToAdd = this;
if (layer.beforeAdd) {
layer.beforeAdd(this);
}
this.whenReady(layer._layerAdd, layer);
return this;
},
removeLayer: function (layer) {
var id = Util.stamp(layer);
if (!this._layers[id]) {
return this;
}
if (this._loaded) {
layer.onRemove(this);
}
delete this._layers[id];
if (this._loaded) {
this.fire("layerremove", { layer: layer });
layer.fire("remove");
}
layer._map = layer._mapToAdd = null;
return this;
},
hasLayer: function (layer) {
return Util.stamp(layer) in this._layers;
},
eachLayer: function (method, context) {
for (var i in this._layers) {
method.call(context, this._layers[i]);
}
return this;
},
_addLayers: function (layers) {
layers = layers ? (Util.isArray(layers) ? layers : [layers]) : [];
for (var i = 0, len = layers.length; i < len; i++) {
this.addLayers(layers[i]);
}
},
_addZoomLimit: function (layer) {
if (!isNaN(layer.options.maxZoom) || !isNaN(layer.options.minZoom)) {
this._zoomBoundLayers[Util.stamp(layer)] = layer;
this._updateZoomLevels();
}
},
_removeZoomLimit: function (layer) {
var id = Util.stamp(layer);
if (this._zoomBoundLayers[id]) {
delete this._zoomBoundLayers[id];
this._updateZoomLevels();
}
},
_updateZoomLevels: function () {
var minZoom = Infinity,
maxZoom = -Infinity,
oldZoomSpan = this._getZoomSpan();
for (var i in this._zoomBoundLayers) {
var options = this._zoomBoundLayers[i].options;
minZoom =
options.minZoom === undefined
? minZoom
: Math.min(minZoom, options.minZoom);
maxZoom =
options.maxZoom == undefined
? maxZoom
: Math.max(maxZoom, options.maxZoom);
}
this._layersMaxZoom = maxZoom === -Infinity ? undefined : maxZoom;
this._layersMinZoom = minZoom === Infinity ? undefined : minZoom;
if (oldZoomSpan !== this._getZoomSpan()) {
this.fire("zoomlevelschange");
}
if (
this.options.maxZoom === undefined &&
this._layersMaxZoom &&
this.getZoom() > this._layersMaxZoom
) {
this.setZoom(this._layersMaxZoom);
}
if (
this.options.minZoom == undefined &&
this._layersMinZoom &&
this.getZoom() < this._layersMinZoom
) {
this.setZoom(this._layersMinZoom);
}
},
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
编辑 (opens new window)
上次更新: 2025/04/15, 05:47:03