3

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

  • can you add how you defined (extent), because you should define it as your map projection and boundaries – Moh Feb 15 '18 at 10:43
  • var ne = ol.proj.fromLonLat([parseFloat(image.north), parseFloat(image.east)]); var sw = ol.proj.fromLonLat([parseFloat(image.south), parseFloat(image.west)]); var extent = [sw[0], sw[1], ne[0], ne[1]]; And these extents work properly with image layer, it only has a problem with tile layer. – Mohit Feb 16 '18 at 6:40
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.