diff --git a/examples/localized-openstreetmap.html b/examples/localized-openstreetmap.html new file mode 100644 index 0000000000..abe0b2aae3 --- /dev/null +++ b/examples/localized-openstreetmap.html @@ -0,0 +1,55 @@ + + + + + + + + + + Localized OpenStreetMap example + + + + + +
+ +
+
+
+
+
+ +
+ +
+

Localized OpenStreetMap example

+

Example of a localized OpenStreetMap map with a custom tile server and a custom attribution.

+
+

See the localized-openstreetmap.js source to see how this is done.

+
+
localized-openstreetmap, openstreetmap
+
+ +
+ +
+ + + + + + diff --git a/examples/localized-openstreetmap.js b/examples/localized-openstreetmap.js new file mode 100644 index 0000000000..458f92bc04 --- /dev/null +++ b/examples/localized-openstreetmap.js @@ -0,0 +1,29 @@ +goog.require('ol.Attribution'); +goog.require('ol.Map'); +goog.require('ol.RendererHints'); +goog.require('ol.View2D'); +goog.require('ol.layer.TileLayer'); +goog.require('ol.source.OpenStreetMap'); + + +var map = new ol.Map({ + layers: [ + new ol.layer.TileLayer({ + source: new ol.source.OpenStreetMap({ + attribution: new ol.Attribution( + 'All maps © ' + + 'OpenCycleMap, ' + + 'map data © ' + + 'OpenStreetMap ' + + '(ODbL)'), + url: 'http://{a-c}.tile.opencyclemap.org/cycle/{z}/{x}/{y}.png' + }) + }) + ], + renderers: ol.RendererHints.createFromQueryData(), + target: 'map', + view: new ol.View2D({ + center: [-172857, 5977746], + zoom: 12 + }) +}); diff --git a/src/objectliterals.exports b/src/objectliterals.exports index 3ec7c4af74..4d2e882759 100644 --- a/src/objectliterals.exports +++ b/src/objectliterals.exports @@ -150,6 +150,12 @@ @exportObjectLiteralProperty ol.source.DebugTileSourceOptions.projection ol.ProjectionLike @exportObjectLiteralProperty ol.source.DebugTileSourceOptions.tileGrid ol.tilegrid.TileGrid|undefined +@exportObjectLiteral ol.source.OpenStreetMapOptions +@exportObjectLiteralProperty ol.source.OpenStreetMapOptions.attribution ol.Attribution|undefined +@exportObjectLiteralProperty ol.source.OpenStreetMapOptions.attributions Array.|undefined +@exportObjectLiteralProperty ol.source.OpenStreetMapOptions.maxZoom number|undefined +@exportObjectLiteralProperty ol.source.OpenStreetMapOptions.url string|undefined + @exportObjectLiteral ol.source.SingleImageWMSOptions @exportObjectLiteralProperty ol.source.SingleImageWMSOptions.attributions Array.|undefined @exportObjectLiteralProperty ol.source.SingleImageWMSOptions.crossOrigin null|string|undefined diff --git a/src/ol/source/openstreetmapsource.js b/src/ol/source/openstreetmapsource.js index 7818574939..5f73770068 100644 --- a/src/ol/source/openstreetmapsource.js +++ b/src/ol/source/openstreetmapsource.js @@ -4,24 +4,46 @@ goog.require('ol.Attribution'); goog.require('ol.source.XYZ'); +/** + * @const + * @type {Array.} + */ +ol.source.OPENSTREETMAP_ATTRIBUTIONS = [new ol.Attribution( + '© OpenStreetMap ' + + 'contributors, ' + + 'CC BY-SA')]; + + /** * @constructor * @extends {ol.source.XYZ} + * @param {ol.source.OpenStreetMapOptions=} opt_options Open Street Map options. */ -ol.source.OpenStreetMap = function() { +ol.source.OpenStreetMap = function(opt_options) { - var attribution = new ol.Attribution( - '© OpenStreetMap ' + - 'contributors, ' + - 'CC BY-SA'); + var options = goog.isDef(opt_options) ? opt_options : {}; + + var attributions; + if (goog.isDef(options.attributions)) { + attributions = options.attributions; + } else if (goog.isDef(options.attribution)) { + attributions = [options.attribution]; + } else { + attributions = ol.source.OPENSTREETMAP_ATTRIBUTIONS; + } + + var maxZoom = goog.isDef(options.maxZoom) ? options.maxZoom : 18; + + var url = goog.isDef(options.url) ? + options.url : 'http://{a-c}.tile.openstreetmap.org/{z}/{x}/{y}.png'; goog.base(this, { - attributions: [attribution], + attributions: attributions, crossOrigin: 'anonymous', opaque: true, - maxZoom: 18, - url: 'http://{a-c}.tile.openstreetmap.org/{z}/{x}/{y}.png' + maxZoom: maxZoom, + url: url }); };