Add geojson setStyle and resetStyle methods, layer feature property

This commit is contained in:
mourner
2012-08-04 10:56:14 +03:00
parent fc1e6669ab
commit 8d6301cf07

View File

@ -20,27 +20,42 @@ L.GeoJSON = L.FeatureGroup.extend({
return this;
}
var options = this.options,
style = options.style;
var options = this.options;
if (options.filter && !options.filter(geojson)) { return; }
var layer = L.GeoJSON.geometryToLayer(geojson, options.pointToLayer);
layer.feature = geojson;
if (style) {
if (typeof style === 'function') {
style = style(geojson);
}
if (layer.setStyle) {
layer.setStyle(style);
}
}
this.resetStyle(layer);
if (options.onEachFeature) {
options.onEachFeature(geojson, layer);
}
return this.addLayer(layer);
},
resetStyle: function (layer) {
var style = this.options.style;
if (style) {
this._setLayerStyle(layer, style);
}
},
setStyle: function (style) {
this.eachLayer(function (layer) {
this._setLayerStyle(layer, style);
}, this);
},
_setLayerStyle: function (layer, style) {
if (typeof style === 'function') {
style = style(layer.feature);
}
if (layer.setStyle) {
layer.setStyle(style);
}
}
});
@ -118,4 +133,4 @@ L.Util.extend(L.GeoJSON, {
L.geoJson = function (geojson, options) {
return new L.GeoJSON(geojson, options);
};
};