[GIS] Extent property is not working with openlayers tile layer

extentsopenlayersvector-tiles

I have an application where the images are load through the tiles, and I successfully made the tiles and was able to show them on the map.
But never was able to set the extent of the tile layer.
Every time I set the properties, the layer doesn't load for the map.

 var mapSource = new ol.source.OSM({
    url: layerUrl    /// Local Tile Source
});
var mapLayer = new ol.layer.Tile({
    source: mapSource,
    zIndex: 1,
    visible: true,
    //   extent: extent,    ///Comment Line 1
});

//  mapLayer.setExtent(extent);   ///Comment Line 2
map.addLayer(mapLayer);

If I comment the Comment Line 1 and 2, tile layers load, but if I uncomment any of one line, tile layer doesn't load.

Do anyone know the exact reason and the solution for it??
P.S : I am using openlayers v4.6.4

Best Answer

After a lot of research and hit & trials. I found out that I was using the wrong map source class to initialize the map Source.

The correct code classes are:

var mapSource = new ol.source.TileImage({
        url: layerUrl
});
var mapLayer = new ol.layer.Tile({
source: mapSource,
        zIndex: parseInt(image.zindex),
        visible: true,
        'imageId': image.imageId
});
map.addLayer(mapLayer);
mapLayer.setExtent(extent);

I hope this will help someone going through the same issue.