[GIS] How to change source url with function

layersopenlayerstiles

I have url for XYZ source tiles. But there are file names like:

/1/1-0.jpg, --> {z}/{x}-{y}.jpg

There is old code for ol2 transform url.

MyClass = OpenLayers.Class(OpenLayers.Layer.XYZ, {
getURL: function (bounds) {
    var xyz = this.getXYZ(bounds); 
/... code .../
    var textY = xyz.y.toString(2);
    while (textY.length < xyz.z) textY = '0' + textY;

    for (var i = 6; i < xyz.z; i++)
            path += textY.substr(0,i-5) + '-' + textX.substr(0,i-5) + '/';

    path += textY + '-' + textX + '.jpg';

    return path;
},

CLASS_NAME: "MyClass"

It is possible to make it in ol3?

Best Answer

In ol.source.XYZ there is a tileUrlFunction parameter (API TileUrlFunctionType) that should be what you are looking for:

var tileSource = source: new ol.source.XYZ({
    //...
    tileUrlFunction: function(tileCoord, pixelRatio, projection){
        // tileCoord is representing the location of a tile in a tile grid (z, x, y)
        var z = tileCoord[0].toString();
        var x = tileCoord[1].toString();
        var y = tileCoord[2].toString();

        // add the part /1/1-0.jpg, --> {z}/{x}-{y}.jpg
        path += '/' + z + '/' + x + '-' + y + '.jpg';
        return path;
    }
});
Related Question