[GIS] Transport layer on OpenLayers 3 map: ol.layer.OSM is not a constructor

layersopenlayersopenstreetmaptilestransportation

I'm using the Thunderforest Transport map and I want to overlay it onto my OpenLayers 3 map. After looking through the documentation and following their example (http://www.thunderforest.com/tutorials/openlayers/), I get the following error:

ol.layer.OSM is not a constructor

Everything in my code is working fine, but the following section raises this error:

var transport = new ol.layer.OSM("TransportMap", ["https://tile.thunderforest.com/transport/0/0/0.png?apikey=MY_API_KEY"]);
map.addLayer(transport);

I looked at the documentation (http://openlayers.org/en/latest/apidoc/ol.layer.html) and it seems that OSM is no longer present in ol.layer. What method do I use to add such a layer to my existing map? Should I add it as a Tile?

Best Answer

The example from the Thunderforest website was made for OpenLayers2.

For OL4, I think you should use ol.layer.Tile with ol.source.XYZ, such as in this example.

 new ol.layer.Tile({
       source: new ol.source.XYZ({
               url: 'https://{a-c}.tile.thunderforest.com/cycle/{z}/{x}/{y}.png' +
                   '?apikey=Your API key from http://www.thunderforest.com/docs/apikeys/ here'
          })
     })
Related Question