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.
This commit is contained in:
@@ -48,7 +48,6 @@
|
|||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script src="jquery.min.js" type="text/javascript"></script>
|
|
||||||
<script src="loader.js?id=kml" type="text/javascript"></script>
|
<script src="loader.js?id=kml" type="text/javascript"></script>
|
||||||
<script src="../resources/social-links.js" type="text/javascript"></script>
|
<script src="../resources/social-links.js" type="text/javascript"></script>
|
||||||
|
|
||||||
|
|||||||
@@ -43,8 +43,19 @@ var map = new ol.Map({
|
|||||||
var kml = new ol.parser.KML({
|
var kml = new ol.parser.KML({
|
||||||
maxDepth: 1, dimension: 2, extractStyles: true, extractAttributes: true});
|
maxDepth: 1, dimension: 2, extractStyles: true, extractAttributes: true});
|
||||||
|
|
||||||
$.ajax({
|
var url = 'data/kml/lines.kml';
|
||||||
url: 'data/kml/lines.kml'
|
var xhr = new XMLHttpRequest();
|
||||||
}).done(function(data) {
|
xhr.open('GET', url, true);
|
||||||
vector.parseFeatures(data, kml, epsg4326);
|
|
||||||
});
|
|
||||||
|
/**
|
||||||
|
* 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();
|
||||||
|
|||||||
@@ -350,10 +350,15 @@ ol.layer.Vector.prototype.parseFeatures = function(data, parser, projection) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (goog.isString(data)) {
|
if (goog.isString(data)) {
|
||||||
goog.asserts.assert(goog.isFunction(parser.readFeaturesFromString),
|
if (goog.isFunction(parser.readFeaturesFromStringAsync)) {
|
||||||
'Expected a parser with readFeaturesFromString method.');
|
parser.readFeaturesFromStringAsync(data, goog.bind(addFeatures, this),
|
||||||
features = parser.readFeaturesFromString(data, {callback: callback});
|
{callback: callback});
|
||||||
addFeatures.call(this, features);
|
} 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)) {
|
} else if (goog.isObject(data)) {
|
||||||
if (goog.isFunction(parser.readFeaturesFromObjectAsync)) {
|
if (goog.isFunction(parser.readFeaturesFromObjectAsync)) {
|
||||||
parser.readFeaturesFromObjectAsync(data, goog.bind(addFeatures, this),
|
parser.readFeaturesFromObjectAsync(data, goog.bind(addFeatures, this),
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
goog.provide('ol.parser.AsyncObjectFeatureParser');
|
goog.provide('ol.parser.AsyncObjectFeatureParser');
|
||||||
|
goog.provide('ol.parser.AsyncStringFeatureParser');
|
||||||
goog.provide('ol.parser.DomFeatureParser');
|
goog.provide('ol.parser.DomFeatureParser');
|
||||||
goog.provide('ol.parser.ObjectFeatureParser');
|
goog.provide('ol.parser.ObjectFeatureParser');
|
||||||
goog.provide('ol.parser.ReadFeaturesOptions');
|
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.<ol.Feature>)} callback Callback which is called
|
||||||
|
* after parsing.
|
||||||
|
* @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options.
|
||||||
|
*/
|
||||||
|
ol.parser.AsyncStringFeatureParser.prototype.readFeaturesFromStringAsync =
|
||||||
|
goog.abstractMethod;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @interface
|
* @interface
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ goog.require('ol.geom.Point');
|
|||||||
goog.require('ol.geom.Polygon');
|
goog.require('ol.geom.Polygon');
|
||||||
goog.require('ol.geom.SharedVertices');
|
goog.require('ol.geom.SharedVertices');
|
||||||
goog.require('ol.parser.AsyncObjectFeatureParser');
|
goog.require('ol.parser.AsyncObjectFeatureParser');
|
||||||
|
goog.require('ol.parser.AsyncStringFeatureParser');
|
||||||
goog.require('ol.parser.DomFeatureParser');
|
goog.require('ol.parser.DomFeatureParser');
|
||||||
goog.require('ol.parser.ReadFeaturesOptions');
|
goog.require('ol.parser.ReadFeaturesOptions');
|
||||||
goog.require('ol.parser.StringFeatureParser');
|
goog.require('ol.parser.StringFeatureParser');
|
||||||
@@ -47,6 +48,7 @@ ol.parser.KMLOptions;
|
|||||||
* @implements {ol.parser.DomFeatureParser}
|
* @implements {ol.parser.DomFeatureParser}
|
||||||
* @implements {ol.parser.StringFeatureParser}
|
* @implements {ol.parser.StringFeatureParser}
|
||||||
* @implements {ol.parser.AsyncObjectFeatureParser}
|
* @implements {ol.parser.AsyncObjectFeatureParser}
|
||||||
|
* @implements {ol.parser.AsyncStringFeatureParser}
|
||||||
* @param {ol.parser.KMLOptions=} opt_options Optional configuration object.
|
* @param {ol.parser.KMLOptions=} opt_options Optional configuration object.
|
||||||
* @extends {ol.parser.XML}
|
* @extends {ol.parser.XML}
|
||||||
*/
|
*/
|
||||||
@@ -818,6 +820,19 @@ ol.parser.KML.prototype.readFeaturesFromObjectAsync =
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} str KML document.
|
||||||
|
* @param {function(Array.<ol.Feature>)} 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.
|
* Parse a KML document provided as a string.
|
||||||
* @param {string} str KML document.
|
* @param {string} str KML document.
|
||||||
|
|||||||
Reference in New Issue
Block a user