Separate out feature reading and feature adding
This commit is contained in:
@@ -95,7 +95,7 @@ var vectorSource = new ol.source.ServerVector({
|
||||
var url = 'http://overpass-api.de/api/xapi?map?bbox=' +
|
||||
epsg4326Extent.join(',');
|
||||
$.ajax(url).then(function(response) {
|
||||
vectorSource.readFeatures(response);
|
||||
vectorSource.addFeatures(vectorSource.readFeatures(response));
|
||||
});
|
||||
},
|
||||
strategy: ol.loadingstrategy.createTile(new ol.tilegrid.XYZ({
|
||||
|
||||
@@ -40,62 +40,18 @@ ol.source.FormatVector = function(options) {
|
||||
*/
|
||||
this.format = options.format;
|
||||
|
||||
/**
|
||||
* @protected
|
||||
* @type {function(Event)}
|
||||
*/
|
||||
this.handleXhrIo = goog.bind(this.handleXhrIo_, this);
|
||||
|
||||
};
|
||||
goog.inherits(ol.source.FormatVector, ol.source.Vector);
|
||||
|
||||
|
||||
/**
|
||||
* @param {Event} event Event.
|
||||
* @private
|
||||
*/
|
||||
ol.source.FormatVector.prototype.handleXhrIo_ = function(event) {
|
||||
var xhrIo = event.target;
|
||||
goog.asserts.assertInstanceof(xhrIo, goog.net.XhrIo);
|
||||
if (xhrIo.isSuccess()) {
|
||||
var type = this.format.getType();
|
||||
/** @type {ArrayBuffer|Document|Node|Object|string|undefined} */
|
||||
var source;
|
||||
if (type == ol.format.FormatType.BINARY &&
|
||||
ol.BrowserFeature.HAS_ARRAY_BUFFER) {
|
||||
source = xhrIo.getResponse();
|
||||
goog.asserts.assertInstanceof(source, ArrayBuffer);
|
||||
} else if (type == ol.format.FormatType.JSON) {
|
||||
source = xhrIo.getResponseText();
|
||||
} else if (type == ol.format.FormatType.TEXT) {
|
||||
source = xhrIo.getResponseText();
|
||||
} else if (type == ol.format.FormatType.XML) {
|
||||
if (!goog.userAgent.IE) {
|
||||
source = xhrIo.getResponseXml();
|
||||
}
|
||||
if (!goog.isDefAndNotNull(source)) {
|
||||
source = ol.xml.load(xhrIo.getResponseText());
|
||||
}
|
||||
} else {
|
||||
goog.asserts.fail();
|
||||
}
|
||||
if (goog.isDefAndNotNull(source)) {
|
||||
this.readFeatures(source);
|
||||
} else {
|
||||
this.setState(ol.source.State.ERROR);
|
||||
goog.asserts.fail();
|
||||
}
|
||||
} else {
|
||||
this.setState(ol.source.State.ERROR);
|
||||
}
|
||||
goog.dispose(xhrIo);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {goog.Uri|string} url URL.
|
||||
* @param {function(this: T, Array.<ol.Feature>)} callback Callback.
|
||||
* @param {T} thisArg Value to use as `this` when executing `callback`.
|
||||
* @template T
|
||||
*/
|
||||
ol.source.FormatVector.prototype.loadFeaturesFromURL = function(url) {
|
||||
ol.source.FormatVector.prototype.loadFeaturesFromURL =
|
||||
function(url, callback, thisArg) {
|
||||
var xhrIo = new goog.net.XhrIo();
|
||||
var type = this.format.getType();
|
||||
var responseType;
|
||||
@@ -107,13 +63,55 @@ ol.source.FormatVector.prototype.loadFeaturesFromURL = function(url) {
|
||||
responseType = goog.net.XhrIo.ResponseType.TEXT;
|
||||
}
|
||||
xhrIo.setResponseType(responseType);
|
||||
goog.events.listen(xhrIo, goog.net.EventType.COMPLETE, this.handleXhrIo);
|
||||
goog.events.listen(xhrIo, goog.net.EventType.COMPLETE,
|
||||
/**
|
||||
* @param {Event} event Event.
|
||||
* @private
|
||||
* @this {ol.source.FormatVector}
|
||||
*/
|
||||
function(event) {
|
||||
var xhrIo = event.target;
|
||||
goog.asserts.assertInstanceof(xhrIo, goog.net.XhrIo);
|
||||
if (xhrIo.isSuccess()) {
|
||||
var type = this.format.getType();
|
||||
/** @type {ArrayBuffer|Document|Node|Object|string|undefined} */
|
||||
var source;
|
||||
if (type == ol.format.FormatType.BINARY &&
|
||||
ol.BrowserFeature.HAS_ARRAY_BUFFER) {
|
||||
source = xhrIo.getResponse();
|
||||
goog.asserts.assertInstanceof(source, ArrayBuffer);
|
||||
} else if (type == ol.format.FormatType.JSON) {
|
||||
source = xhrIo.getResponseText();
|
||||
} else if (type == ol.format.FormatType.TEXT) {
|
||||
source = xhrIo.getResponseText();
|
||||
} else if (type == ol.format.FormatType.XML) {
|
||||
if (!goog.userAgent.IE) {
|
||||
source = xhrIo.getResponseXml();
|
||||
}
|
||||
if (!goog.isDefAndNotNull(source)) {
|
||||
source = ol.xml.load(xhrIo.getResponseText());
|
||||
}
|
||||
} else {
|
||||
goog.asserts.fail();
|
||||
}
|
||||
if (goog.isDefAndNotNull(source)) {
|
||||
callback.call(thisArg, this.readFeatures(source));
|
||||
} else {
|
||||
this.setState(ol.source.State.ERROR);
|
||||
goog.asserts.fail();
|
||||
}
|
||||
} else {
|
||||
this.setState(ol.source.State.ERROR);
|
||||
}
|
||||
goog.dispose(xhrIo);
|
||||
}, false, this);
|
||||
xhrIo.send(url);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
|
||||
* @return {Array.<ol.Feature>} Features.
|
||||
*/
|
||||
ol.source.FormatVector.prototype.readFeatures = function(source) {
|
||||
var format = this.format;
|
||||
@@ -133,6 +131,5 @@ ol.source.FormatVector.prototype.readFeatures = function(source) {
|
||||
}
|
||||
}
|
||||
}
|
||||
this.addFeaturesInternal(features);
|
||||
this.setState(ol.source.State.READY);
|
||||
return features;
|
||||
};
|
||||
|
||||
@@ -23,35 +23,51 @@ ol.source.StaticVector = function(options) {
|
||||
});
|
||||
|
||||
if (goog.isDef(options.arrayBuffer)) {
|
||||
this.readFeatures(options.arrayBuffer);
|
||||
this.addFeaturesInternal(this.readFeatures(options.arrayBuffer));
|
||||
}
|
||||
|
||||
if (goog.isDef(options.doc)) {
|
||||
this.readFeatures(options.doc);
|
||||
this.addFeaturesInternal(this.readFeatures(options.doc));
|
||||
}
|
||||
|
||||
if (goog.isDef(options.node)) {
|
||||
this.readFeatures(options.node);
|
||||
this.addFeaturesInternal(this.readFeatures(options.node));
|
||||
}
|
||||
|
||||
if (goog.isDef(options.object)) {
|
||||
this.readFeatures(options.object);
|
||||
this.addFeaturesInternal(this.readFeatures(options.object));
|
||||
}
|
||||
|
||||
if (goog.isDef(options.text)) {
|
||||
this.readFeatures(options.text);
|
||||
this.addFeaturesInternal(this.readFeatures(options.text));
|
||||
}
|
||||
|
||||
if (goog.isDef(options.url) || goog.isDef(options.urls)) {
|
||||
this.setState(ol.source.State.LOADING);
|
||||
if (goog.isDef(options.url)) {
|
||||
this.loadFeaturesFromURL(options.url);
|
||||
this.loadFeaturesFromURL(options.url,
|
||||
/**
|
||||
* @param {Array.<ol.Feature>} features Features.
|
||||
* @this {ol.source.StaticVector}
|
||||
*/
|
||||
function(features) {
|
||||
this.addFeaturesInternal(features);
|
||||
this.setState(ol.source.State.READY);
|
||||
}, this);
|
||||
}
|
||||
if (goog.isDef(options.urls)) {
|
||||
var urls = options.urls;
|
||||
var i, ii;
|
||||
for (i = 0, ii = urls.length; i < ii; ++i) {
|
||||
this.loadFeaturesFromURL(urls[i]);
|
||||
this.loadFeaturesFromURL(urls[i],
|
||||
/**
|
||||
* @param {Array.<ol.Feature>} features Features.
|
||||
* @this {ol.source.StaticVector}
|
||||
*/
|
||||
function(features) {
|
||||
this.addFeaturesInternal(features);
|
||||
this.setState(ol.source.State.READY);
|
||||
}, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user