Support GeoJSON as parsed object

With this change, we can programmatically create
GeoJSONFeatureCollection objects programmatically, without
reading a GeoJSON file.
This commit is contained in:
ahocevar
2013-03-08 16:25:23 +01:00
parent 7f62b26c80
commit 389b8d14d7
2 changed files with 17 additions and 0 deletions

View File

@@ -323,6 +323,10 @@ ol.layer.Vector.prototype.parseFeatures = function(data, parser, projection) {
goog.asserts.assert(typeof parser.readFeaturesFromString === 'function',
'Expected a parser with readFeaturesFromString method.');
features = parser.readFeaturesFromString(data, {callback: callback});
} else if (typeof data === 'object') {
goog.asserts.assert(typeof parser.readFeaturesFromObject === 'function',
'Expected a parser with a readFeaturesFromObject method.');
features = parser.readFeaturesFromObject(data, {callback: callback});
} else {
// TODO: parse more data types
throw new Error('Data type not supported: ' + data);

View File

@@ -51,6 +51,19 @@ ol.parser.GeoJSON.prototype.readFeaturesFromString =
};
/**
* Parse a GeoJSON feature collection from decoded JSON.
* @param {GeoJSONFeatureCollection} object GeoJSON feature collection decoded
* from JSON.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {Array.<ol.Feature>} Array of features.
*/
ol.parser.GeoJSON.prototype.readFeaturesFromObject =
function(object, opt_options) {
return this.parseFeatureCollection_(object, opt_options);
};
/**
* @param {GeoJSONObject} json GeoJSON object.
* @return {ol.Feature|Array.<ol.Feature>|