From feb6c4c2d75c348cade29d24a48dd48070bb61e5 Mon Sep 17 00:00:00 2001 From: Bart van den Eijnden Date: Mon, 22 Apr 2013 16:45:33 +0200 Subject: [PATCH] Add AsyncStringFeatureParser and use plain XMLHttpRequest in the KML example Since the content-type on github.io is application/octet-stream we need to implement an Async string based parser interface in the KML parser. Also use plain XmlHttpRequest in the example instead of jQuery Ajax since the vector-features example also uses that. --- examples/kml.html | 1 - examples/kml.js | 21 ++++++++++++++++----- src/ol/layer/vectorlayer.js | 13 +++++++++---- src/ol/parser/featureparser.js | 18 ++++++++++++++++++ src/ol/parser/kml.js | 15 +++++++++++++++ 5 files changed, 58 insertions(+), 10 deletions(-) diff --git a/examples/kml.html b/examples/kml.html index 467dbc1485..4c243a0950 100644 --- a/examples/kml.html +++ b/examples/kml.html @@ -48,7 +48,6 @@ - diff --git a/examples/kml.js b/examples/kml.js index 8662b0a508..dbbeb5da6e 100644 --- a/examples/kml.js +++ b/examples/kml.js @@ -43,8 +43,19 @@ var map = new ol.Map({ var kml = new ol.parser.KML({ maxDepth: 1, dimension: 2, extractStyles: true, extractAttributes: true}); -$.ajax({ - url: 'data/kml/lines.kml' -}).done(function(data) { - vector.parseFeatures(data, kml, epsg4326); -}); +var url = 'data/kml/lines.kml'; +var xhr = new XMLHttpRequest(); +xhr.open('GET', url, true); + + +/** + * onload handler for the XHR request. + */ +xhr.onload = function() { + if (xhr.status == 200) { + // this is silly to have to tell the layer the destination projection + var projection = map.getView().getProjection(); + vector.parseFeatures(xhr.responseText, kml, epsg4326); + } +}; +xhr.send(); diff --git a/src/ol/layer/vectorlayer.js b/src/ol/layer/vectorlayer.js index 7fbc99feb2..fd5f022f46 100644 --- a/src/ol/layer/vectorlayer.js +++ b/src/ol/layer/vectorlayer.js @@ -350,10 +350,15 @@ ol.layer.Vector.prototype.parseFeatures = function(data, parser, projection) { }; if (goog.isString(data)) { - goog.asserts.assert(goog.isFunction(parser.readFeaturesFromString), - 'Expected a parser with readFeaturesFromString method.'); - features = parser.readFeaturesFromString(data, {callback: callback}); - addFeatures.call(this, features); + if (goog.isFunction(parser.readFeaturesFromStringAsync)) { + parser.readFeaturesFromStringAsync(data, goog.bind(addFeatures, this), + {callback: callback}); + } else { + goog.asserts.assert(goog.isFunction(parser.readFeaturesFromString), + 'Expected a parser with readFeaturesFromString method.'); + features = parser.readFeaturesFromString(data, {callback: callback}); + addFeatures.call(this, features); + } } else if (goog.isObject(data)) { if (goog.isFunction(parser.readFeaturesFromObjectAsync)) { parser.readFeaturesFromObjectAsync(data, goog.bind(addFeatures, this), diff --git a/src/ol/parser/featureparser.js b/src/ol/parser/featureparser.js index 94b828f2a7..9313e23288 100644 --- a/src/ol/parser/featureparser.js +++ b/src/ol/parser/featureparser.js @@ -1,4 +1,5 @@ goog.provide('ol.parser.AsyncObjectFeatureParser'); +goog.provide('ol.parser.AsyncStringFeatureParser'); goog.provide('ol.parser.DomFeatureParser'); goog.provide('ol.parser.ObjectFeatureParser'); goog.provide('ol.parser.ReadFeaturesOptions'); @@ -56,6 +57,23 @@ ol.parser.StringFeatureParser.prototype.readFeaturesFromString = +/** + * @interface + */ +ol.parser.AsyncStringFeatureParser = function() {}; + + +/** + * @param {string} data String data. + * @param {function(Array.)} callback Callback which is called + * after parsing. + * @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options. + */ +ol.parser.AsyncStringFeatureParser.prototype.readFeaturesFromStringAsync = + goog.abstractMethod; + + + /** * @interface */ diff --git a/src/ol/parser/kml.js b/src/ol/parser/kml.js index 93566c42f5..7102a23218 100644 --- a/src/ol/parser/kml.js +++ b/src/ol/parser/kml.js @@ -21,6 +21,7 @@ goog.require('ol.geom.Point'); goog.require('ol.geom.Polygon'); goog.require('ol.geom.SharedVertices'); goog.require('ol.parser.AsyncObjectFeatureParser'); +goog.require('ol.parser.AsyncStringFeatureParser'); goog.require('ol.parser.DomFeatureParser'); goog.require('ol.parser.ReadFeaturesOptions'); goog.require('ol.parser.StringFeatureParser'); @@ -47,6 +48,7 @@ ol.parser.KMLOptions; * @implements {ol.parser.DomFeatureParser} * @implements {ol.parser.StringFeatureParser} * @implements {ol.parser.AsyncObjectFeatureParser} + * @implements {ol.parser.AsyncStringFeatureParser} * @param {ol.parser.KMLOptions=} opt_options Optional configuration object. * @extends {ol.parser.XML} */ @@ -818,6 +820,19 @@ ol.parser.KML.prototype.readFeaturesFromObjectAsync = }; +/** + * @param {string} str KML document. + * @param {function(Array.)} callback Callback which is called + * after parsing. + * @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options. + */ +ol.parser.KML.prototype.readFeaturesFromStringAsync = + function(str, callback, opt_options) { + this.readFeaturesOptions_ = opt_options; + this.read(str, callback); +}; + + /** * Parse a KML document provided as a string. * @param {string} str KML document.