From 8ba830f91fa2a191e58bc782b575a337209f18ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Mon, 24 Nov 2014 14:42:21 +0100 Subject: [PATCH 1/2] Make loadFeaturesFromUrl accept an error callback --- src/ol/source/formatvectorsource.js | 12 +++++++----- src/ol/source/staticvectorsource.js | 16 +++++++++++++--- src/ol/source/tilevectorsource.js | 21 +++++++++++---------- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/ol/source/formatvectorsource.js b/src/ol/source/formatvectorsource.js index 1ceba4d297..7c94bf5fa2 100644 --- a/src/ol/source/formatvectorsource.js +++ b/src/ol/source/formatvectorsource.js @@ -48,12 +48,14 @@ goog.inherits(ol.source.FormatVector, ol.source.Vector); /** * @param {goog.Uri|string} url URL. - * @param {function(this: T, Array.)} callback Callback. - * @param {T} thisArg Value to use as `this` when executing `callback`. + * @param {function(this: T, Array.)} success Success Callback. + * @param {function(this: T)} error Error callback. + * @param {T} thisArg Value to use as `this` when executing `success` or + * `error`. * @template T */ ol.source.FormatVector.prototype.loadFeaturesFromURL = - function(url, callback, thisArg) { + function(url, success, error, thisArg) { var xhrIo = new goog.net.XhrIo(); var type = this.format.getType(); var responseType; @@ -97,13 +99,13 @@ ol.source.FormatVector.prototype.loadFeaturesFromURL = goog.asserts.fail(); } if (goog.isDefAndNotNull(source)) { - callback.call(thisArg, this.readFeatures(source)); + success.call(thisArg, this.readFeatures(source)); } else { this.setState(ol.source.State.ERROR); goog.asserts.fail(); } } else { - this.setState(ol.source.State.ERROR); + error.call(thisArg); } goog.dispose(xhrIo); }, false, this); diff --git a/src/ol/source/staticvectorsource.js b/src/ol/source/staticvectorsource.js index ca5c5362c6..30b5e330e7 100644 --- a/src/ol/source/staticvectorsource.js +++ b/src/ol/source/staticvectorsource.js @@ -48,13 +48,15 @@ ol.source.StaticVector = function(options) { 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.onFeaturesLoaded_, this); + this.loadFeaturesFromURL(options.url, + this.onFeaturesLoadedSuccess_, this.onFeaturesLoadedError_, 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.onFeaturesLoaded_, this); + this.loadFeaturesFromURL(urls[i], + this.onFeaturesLoadedSuccess_, this.onFeaturesLoadedError_, this); } } } @@ -63,11 +65,19 @@ ol.source.StaticVector = function(options) { goog.inherits(ol.source.StaticVector, ol.source.FormatVector); +/** + * @private + */ +ol.source.StaticVector.prototype.onFeaturesLoadedError_ = function() { + this.setState(ol.source.State.ERROR); +}; + + /** * @param {Array.} features Features. * @private */ -ol.source.StaticVector.prototype.onFeaturesLoaded_ = function(features) { +ol.source.StaticVector.prototype.onFeaturesLoadedSuccess_ = function(features) { this.addFeaturesInternal(features); this.setState(ol.source.State.READY); }; diff --git a/src/ol/source/tilevectorsource.js b/src/ol/source/tilevectorsource.js index 1dc0ed79db..b0da2796aa 100644 --- a/src/ol/source/tilevectorsource.js +++ b/src/ol/source/tilevectorsource.js @@ -184,6 +184,15 @@ ol.source.TileVector.prototype.loadFeatures = var tileRange = tileGrid.getTileRangeForExtentAndZ(extent, z); var tileCoord = [z, 0, 0]; var x, y; + /** + * @param {string} tileKey Tile key. + * @param {Array.} features Features. + * @this {ol.source.TileVector} + */ + function success(tileKey, features) { + tiles[tileKey] = features; + this.setState(ol.source.State.READY); + } for (x = tileRange.minX; x <= tileRange.maxX; ++x) { for (y = tileRange.minY; y <= tileRange.maxY; ++y) { var tileKey = this.getTileKeyZXY_(z, x, y); @@ -195,16 +204,8 @@ ol.source.TileVector.prototype.loadFeatures = var url = tileUrlFunction(tileCoord, 1, projection); if (goog.isDef(url)) { tiles[tileKey] = []; - this.loadFeaturesFromURL(url, goog.partial( - /** - * @param {string} tileKey Tile key. - * @param {Array.} features Features. - * @this {ol.source.TileVector} - */ - function(tileKey, features) { - tiles[tileKey] = features; - this.setState(ol.source.State.READY); - }, tileKey), this); + this.loadFeaturesFromURL(url, goog.partial(success, tileKey), + goog.nullFunction, this); } } } From ec01aa45b948c063271c57d0e811141c3272d669 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Mon, 24 Nov 2014 14:55:49 +0100 Subject: [PATCH 2/2] Make createTileCoordTransform return identity func This makes ol.tilegrid.TileGrid#createTileCoordTransform return the identity function. This makes it possible to use ol.source.TileVector with an ol.tilegrid.TileGrid (as opposed to an ol.tilegrid.XYZ). --- src/ol/tilegrid/tilegrid.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/ol/tilegrid/tilegrid.js b/src/ol/tilegrid/tilegrid.js index d0b63a6975..a91a78fc02 100644 --- a/src/ol/tilegrid/tilegrid.js +++ b/src/ol/tilegrid/tilegrid.js @@ -2,6 +2,7 @@ goog.provide('ol.tilegrid.TileGrid'); goog.require('goog.array'); goog.require('goog.asserts'); +goog.require('goog.functions'); goog.require('ol'); goog.require('ol.Coordinate'); goog.require('ol.TileCoord'); @@ -101,12 +102,16 @@ ol.tilegrid.TileGrid.tmpTileCoord_ = [0, 0, 0]; /** + * Returns the identity function. May be overridden in subclasses. * @param {{extent: (ol.Extent|undefined), * wrapX: (boolean|undefined)}=} opt_options Options. * @return {function(ol.TileCoord, ol.proj.Projection, ol.TileCoord=): * ol.TileCoord} Tile coordinate transform. */ -ol.tilegrid.TileGrid.prototype.createTileCoordTransform = goog.abstractMethod; +ol.tilegrid.TileGrid.prototype.createTileCoordTransform = + function(opt_options) { + return goog.functions.identity; +}; /**