2

Problem: I am designing an ol3 plugin that needs to copy the URL parameter from ol.source.XYZ/ol.source.TileWMS to ol.layer.Tile on map initialization.

TileWMS: As per the docs, this is easily done with the following.
Note: not actual code, simplified for example. Assumes all layers are TileWMS with no layer groups.

var layers = this.getLayers().getArray();
for (var i = 0; i < layers.length; i++) {
  layer = layers[i];
  layer.set('url', layer.getSource().getUrls()[0]);
}

XYZ: In the docs, I see a setter but no getter. Currently I'm using layer.getSource().setTileLoadFunction() to gain access to the imgSrc parameter, which is the actual URL orf a loaded tile, and genericize it back into a template URL. This works with a single layer paired with one source, but breaks when multiple layers share the same source.
Note: Same assumptions as above.

var layers = this.getLayers().getArray();
for (var i = 0; i < layers.length; i++) {
  layer = layers[i];
  var oldGetTileLoadFunction = layer.getSource().getTileLoadFunction();
  layer.getSource().setTileLoadFunction(function (imgTile, imgSrc) {
    imgTile.getImage().src = imgSrc;

    layer.set('url', MyApp.utils.generateXYZUrl(imgSrc));
    layer.getSource().setTileLoadFunction(oldGetTileLoadFunction);
  });
}

Is there any way to have something like layer.getSource().getUrl() for XYZ? Note: must use native javascript for solution, no external libraries allowed

1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.