fix layer.addTo(group), add layer removeFrom, #2420

This commit is contained in:
Vladimir Agafonkin 2014-02-24 20:18:44 +02:00
parent 229aa1d667
commit b1f70b5c1c
3 changed files with 69 additions and 68 deletions

View File

@ -33,28 +33,26 @@
function populate() { function populate() {
for (var i = 0; i < 10; i++) { for (var i = 0; i < 10; i++) {
markers.addLayer(new L.Marker(getRandomLatLng(map))); L.marker(getRandomLatLng(map)).addTo(markers);
} }
return false; return false;
} }
markers.bindPopup("<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.</p><p>Donec nec justo eget felis facilisis fermentum. Aliquam porttitor mauris sit amet orci. Aenean dignissim pellentesque.</p>"); markers.bindPopup("<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.</p><p>Donec nec justo eget felis facilisis fermentum. Aliquam porttitor mauris sit amet orci. Aenean dignissim pellentesque.</p>").addTo(map);
map.addLayer(markers);
populate(); populate();
L.DomUtil.get('populate').onclick = populate; L.DomUtil.get('populate').onclick = populate;
function logEvent(e) { console.log(e.type); } function logEvent(e) { console.log(e.type); }
map.on('click', logEvent); // map.on('click', logEvent);
//
// map.on('movestart', logEvent); // map.on('movestart', logEvent);
// map.on('move', logEvent); // map.on('move', logEvent);
// map.on('moveend', logEvent); // map.on('moveend', logEvent);
//
// map.on('zoomstart', logEvent); // map.on('zoomstart', logEvent);
// map.on('zoomend', logEvent); // map.on('zoomend', logEvent);
</script> </script>
</body> </body>

View File

@ -6,30 +6,33 @@ L.Layer = L.Evented.extend({
}, },
addTo: function (map) { addTo: function (map) {
var id = L.stamp(this); map.addLayer(this);
if (map._layers[id]) { return this; }
map._layers[id] = this;
this._zoomAnimated = map._zoomAnimated;
if (this.beforeAdd) {
this.beforeAdd(map);
}
this._mapToAdd = map;
map.whenReady(this._layerAdd, this);
return this; return this;
}, },
_layerAdd: function () { remove: function () {
var map = this._mapToAdd; 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);
},
_layerAdd: function (e) {
var map = e.target;
// check in case layer gets added and then removed before the map is ready // check in case layer gets added and then removed before the map is ready
if (!map) { return; } if (!map.hasLayer(this)) { return; }
this._map = map; this._map = map;
this._mapToAdd = null; this._zoomAnimated = map._zoomAnimated;
this.onAdd(map); this.onAdd(map);
@ -43,53 +46,53 @@ L.Layer = L.Evented.extend({
this.fire('add'); this.fire('add');
map.fire('layeradd', {layer: this}); map.fire('layeradd', {layer: this});
},
remove: function () {
var id = L.stamp(this),
map = this._map || this._mapToAdd;
if (!map || !map._layers[id]) { return this; }
if (map._loaded) {
this.onRemove(map);
}
if (this.getAttribution && map.attributionControl) {
map.attributionControl.removeAttribution(this.getAttribution());
}
if (this.getEvents) {
map.off(this.getEvents(), this);
}
delete map._layers[id];
if (map._loaded) {
map.fire('layerremove', {layer: this});
this.fire('remove');
}
this._map = this._mapToAdd = null;
return this;
},
getPane: function (name) {
return this._map.getPane(name ? (this.options[name] || name) : this.options.pane);
} }
}); });
L.Map.include({ L.Map.include({
addLayer: function (layer) { addLayer: function (layer) {
layer.addTo(this); var id = L.stamp(layer);
if (this._layers[id]) { return layer; }
this._layers[id] = layer;
layer._mapToAdd = this;
if (layer.beforeAdd) {
layer.beforeAdd(this);
}
this.whenReady(layer._layerAdd, layer);
return this; return this;
}, },
removeLayer: function (layer) { removeLayer: function (layer) {
layer.remove(); var id = L.stamp(layer);
if (!this._layers[id]) { return this; }
if (this._loaded) {
layer.onRemove(this);
}
if (layer.getAttribution && this.attributionControl) {
this.attributionControl.removeAttribution(layer.getAttribution());
}
if (layer.getEvents) {
this.off(layer.getEvents(), layer);
}
delete this._layers[id];
if (this._loaded) {
this.fire('layerremove', {layer: layer});
layer.fire('remove');
}
layer._map = layer._mapToAdd = null;
return this; return this;
}, },

View File

@ -598,7 +598,7 @@ L.Map = L.Evented.extend({
whenReady: function (callback, context) { whenReady: function (callback, context) {
if (this._loaded) { if (this._loaded) {
callback.call(context || this, this); callback.call(context || this, {target: this});
} else { } else {
this.on('load', callback, context); this.on('load', callback, context);
} }