All tiled layers share some common properties. They inherit from the ol.source.TileImage. Those common properties are quite useful for other ol.source.* that inherit from it, for example, in some cases to put a logo or give attributions (also called credits) for the maps data and/or tiles. You have to be careful in the API docs online as these properties are considered unstable. You have to uncheck the Stable Only checkbox on the top banner. Unstable doesn't mean that you don't have to use those properties but that they may change with future OpenLayers releases. It's very useful for application developers to see what they may need to use or migrate in the future. See the following table for further information:
The most important functions are mostly those that do not start with the word tile. The tileClass, tileGrid, tileLoadFunction and tilePixelRatio exist but we choose to not cover them as they seem out of the scope for beginners; they help setting access to backend with particular tile grids. Along this book, we will sometimes refer to source as layer but it means a layer with the source adapted for a particular backend.
OpenStreetMap (OSM) is a free, Wikipedia style map of the world driven by user contributed content. You are able to use your own OSM tiles or ones provided through the OpenStreetMap servers.
Setting up an OpenStreetMap service and tiles yourself is not too difficult, but it is outside the scope of this book (visit http://switch2osm.org for more information on this). Accessing OSM with OpenLayers, however, isn't.
More information on the OpenStreetMap project can be found at http://www.openstreetmap.org. To access OpenStreetMap, only using the ol.source.OSM constructor without any options is enough. In the previous examples, we were already using this source, so we will not give you another similar example.
Though using constructor without options can be enough, you can also use other properties. The default constructor code uses the publicly available OSM tiles, but it is easy to point it at your own tiles. To do so, create the layer in this format:
var osmLayer = new ol.layer.Tile({
source: new ol.source.OSM({
url: 'http://{a-c}.tile.opencyclemap.org/cycle/{z}/{x}/{y}.png'
})
});To use this, you will have to replace http://{a-c}.tile.opencyclemap.org/cycle/ with the server hosting your OSM tiles. {z}, {x}, and {y} are variables that OpenLayers will replace with the appropriate values to reference specific map tiles.
The {a-c} parameters mean to say you can access tiles using these URLs:
http://a.tile.opencyclemap.org/cycle,
http://b.tile.opencyclemap.org/cycle,
http://c.tile.opencyclemap.org/cycle
This behavior comes from restrictions from your browser: you can't simultaneously ask images from a same domain. It can only request a certain number of resources from the same domain simultaneously, usually 2 or 4. Using subdomains is a way to bypass this limit and speed up the map displaying.
If you have different sources for tiling, it is not only to provide different URLs to access different map images. The way tiling is done depends on established standards.
For example, Stamen layers, OpenStreetMap layers, and MapQuest layers are using the same tiling convention, whereas WMTS (Web Map Tile Service) can adopt other patterns to display tiles. These patterns are set with the ol.tilegrid.TileGrid object that describe for each arbitrary zoomlevel how the tiles are split.
The rules are the following. In reality, the Earth is not a sphere, but in this case, our planet is assimilated to a sphere because we are using Spherical Mercator projection (mainly known as EPSG 900913 or EPSG 3857). Do not worry about projections at the moment, we will review them in a later chapter as it can be a difficult topic to understand.
The entire sphere is projected on a single square at the 0 level. Then, for each zoom, you increase a level and for each tile at the previous zoom, we get 4 tiles at this one.

With the preceding diagram, you will know how to access a tile covering East Europe such as Poland at the 3 level. The URL always looks like URL_TO_TILES/level_z/x_in_grid_for_level/y_for_grid_in_level.png. So, for Poland that has a zoom level 3, x equal to 4, an y to 2, the URL could be http://b.tile.openstreetmap.org/3/4/2.png or http://c.tile.openstreetmap.org/3/4/2.png to get the right tile.
This source enables your layer to consume OpenStreetMap data and customize OpenStreetMap specific properties.
The constructor is ol.source.OSM and its properties can use following options:
MapQuest was, until around 2008, the leading provider for web mapping API. With the entry of Google Maps, the leadership was lost. In 2010, the company chose to change its position concerning web mapping API, by choosing to exclusively rely on OpenStreetMap data to do the job.
Two layers are available freely. They also provide other services such as their own web mapping API. You can get further information at the Open developer part website at http://developer.mapquest.com/web/products/open/.
The MapQuest source class refers directly to the MapQuest tiles server URL.
The constructor is ol.source.MapQuest.
You are only required to use the source constructor included in an ol.layer.Tile object to add the layer. There are only two options' properties. The first one is the layer. It can take values such as osm (roads), sat (satellite), and hyb (hybride) depending on the tiles you want to display. The other property tileLoadFunction will not be covered here: it's not a common requirement for beginners and we already listed it in the ol.source.OSM. At least, you just need to know that it helps change call to tiles with rules defined in your code.
setVisible to be able to see one layer and hide it to show the other underlying layer.You will get something like the following screenshot if you center on the Statue of Liberty and use the MapQuest OSM layer:

The Stamen layers were named so after a company. To cite the OpenstreetMap wiki, Stamen (http://stamen.com) is a San Francisco design and development studio focused on data visualization and map-making. These layers extensively use OpenStreetMap data in many of their map visualizations and have provided three CC-BY OpenStreetMap tilesets: Toner, Terrain, and Watercolor. You can see how the styles look at the official demo website, http://maps.stamen.com.