diff --git a/examples/vector-osm.js b/examples/vector-osm.js index 30526090f5..86f0c02181 100644 --- a/examples/vector-osm.js +++ b/examples/vector-osm.js @@ -87,19 +87,13 @@ var styles = { } }; -var osmxmlFormat = new ol.format.OSMXML(); - var vectorSource = new ol.source.Vector({ - loader: function(extent, resolution, projection) { + format: new ol.format.OSMXML(), + url: function(extent, resolution, projection) { var epsg4326Extent = ol.proj.transformExtent(extent, projection, 'EPSG:4326'); - var url = 'http://overpass-api.de/api/xapi?map?bbox=' + + return 'http://overpass-api.de/api/xapi?map?bbox=' + epsg4326Extent.join(','); - $.ajax(url).then(function(response) { - var features = osmxmlFormat.readFeatures(response, - {featureProjection: projection}); - vectorSource.addFeatures(features); - }); }, strategy: ol.loadingstrategy.tile(ol.tilegrid.createXYZ({ maxZoom: 19 diff --git a/examples/vector-wfs.js b/examples/vector-wfs.js index 3b3d9470b4..eb9bede9e4 100644 --- a/examples/vector-wfs.js +++ b/examples/vector-wfs.js @@ -10,18 +10,14 @@ goog.require('ol.style.Stroke'); goog.require('ol.style.Style'); -// format used to parse WFS GetFeature responses -var geojsonFormat = new ol.format.GeoJSON(); var vectorSource = new ol.source.Vector({ - loader: function(extent, resolution, projection) { - var url = 'http://demo.boundlessgeo.com/geoserver/wfs?service=WFS&' + + format: new ol.format.GeoJSON(), + url: function(extent, resolution, projection) { + return 'http://demo.boundlessgeo.com/geoserver/wfs?service=WFS&' + 'version=1.1.0&request=GetFeature&typename=osm:water_areas&' + - 'outputFormat=text/javascript&format_options=callback:loadFeatures' + - '&srsname=EPSG:3857&bbox=' + extent.join(',') + ',EPSG:3857'; - // use jsonp: false to prevent jQuery from adding the "callback" - // parameter to the URL - $.ajax({url: url, dataType: 'jsonp', jsonp: false}); + 'outputFormat=application/json&srsname=EPSG:3857&' + + 'bbox=' + extent.join(',') + ',EPSG:3857'; }, strategy: ol.loadingstrategy.tile(ol.tilegrid.createXYZ({ maxZoom: 19 @@ -29,14 +25,6 @@ var vectorSource = new ol.source.Vector({ }); -/** - * JSONP WFS callback function. - * @param {Object} response The response object. - */ -window.loadFeatures = function(response) { - vectorSource.addFeatures(geojsonFormat.readFeatures(response)); -}; - var vector = new ol.layer.Vector({ source: vectorSource, style: new ol.style.Style({ diff --git a/externs/olx.js b/externs/olx.js index 343a750975..b54c279332 100644 --- a/externs/olx.js +++ b/externs/olx.js @@ -5195,7 +5195,7 @@ olx.source.TileWMSOptions.prototype.wrapX; * loader: (ol.FeatureLoader|undefined), * logo: (string|olx.LogoOptions|undefined), * strategy: (ol.LoadingStrategy|undefined), - * url: (string|undefined), + * url: (string|ol.FeatureUrlFunction|undefined), * useSpatialIndex: (boolean|undefined), * wrapX: (boolean|undefined)}} * @api @@ -5258,10 +5258,12 @@ olx.source.VectorOptions.prototype.strategy; /** * Setting this option instructs the source to use an XHR loader (see - * {@link ol.featureloader.xhr}) and an {@link ol.loadingstrategy.all} for a - * one-off download of all features from that URL. + * {@link ol.featureloader.xhr}). Use a `string` and an + * {@link ol.loadingstrategy.all} for a one-off download of all features from + * the given URL. Use a {@link ol.FeatureUrlFunction} to generate the url with + * other loading strategies. * Requires `format` to be set as well. - * @type {string|undefined} + * @type {string|ol.FeatureUrlFunction|undefined} * @api */ olx.source.VectorOptions.prototype.url; diff --git a/src/ol/featureloader.js b/src/ol/featureloader.js index f760ac4600..4dbbfec3dc 100644 --- a/src/ol/featureloader.js +++ b/src/ol/featureloader.js @@ -1,4 +1,5 @@ goog.provide('ol.FeatureLoader'); +goog.provide('ol.FeatureUrlFunction'); goog.provide('ol.featureloader'); goog.require('goog.asserts'); @@ -11,6 +12,16 @@ goog.require('ol.xml'); /** + * {@link ol.source.Vector} sources use a function of this type to load + * features. + * + * This function takes an {@link ol.Extent} representing the area to be loaded, + * a `{number}` representing the resolution (map units per pixel) and an + * {@link ol.proj.Projection} for the projection as arguments. `this` within + * the function is bound to the {@link ol.source.Vector} it's called from. + * + * The function is responsible for loading the features and adding them to the + * source. * @api * @typedef {function(this:ol.source.Vector, ol.Extent, number, * ol.proj.Projection)} @@ -19,7 +30,21 @@ ol.FeatureLoader; /** - * @param {string} url Feature URL service. + * {@link ol.source.Vector} sources use a function of this type to get the url + * to load features from. + * + * This function takes an {@link ol.Extent} representing the area to be loaded, + * a `{number}` representing the resolution (map units per pixel) and an + * {@link ol.proj.Projection} for the projection as arguments and returns a + * `{string}` representing the URL. + * @api + * @typedef {function(ol.Extent, number, ol.proj.Projection) : string} + */ +ol.FeatureUrlFunction; + + +/** + * @param {string|ol.FeatureUrlFunction} url Feature URL service. * @param {ol.format.Feature} format Feature format. * @param {function(this:ol.source.Vector, Array.