ismyrnow / Leaflet.functionaltilelayer

Leaflet tile layer with functionally defined URL and support for promises.
MIT License
72 stars 15 forks source link

Virtual Pull Request: MaxNativeResolution #3

Closed DrYSG closed 11 years ago

DrYSG commented 11 years ago

I seem to have some security issues with issuing pull requests, but here is my fixed up source:

L.TileLayer.Functional = L.TileLayer.extend({

    _tileFunction: null,

    initialize: function (tileFunction, options) {
        this._tileFunction = tileFunction;
        L.Util.setOptions(this, options);
    },

    getTileUrl: function (tilePoint) {
        // Note: bbox code untested; pulled from TileLayer.WMS
        var map = this._map,
            crs = map.options.crs,
            tileSize = this.options.tileSize,
            //zoom = this._map.getZoom(), // remove
            zoom = tilePoint.z,  // add
            nwPoint = tilePoint.multiplyBy(tileSize),
            sePoint = nwPoint.add(new L.Point(tileSize, tileSize)),
            nw = crs.project(map.unproject(nwPoint, zoom)),
            se = crs.project(map.unproject(sePoint, zoom)),
            bbox = [nw.x, se.y, se.x, nw.y].join(','),
            view = {
                bbox: bbox,
                width: tileSize,
                height: tileSize,
                zoom: zoom,
                tile: {
                    row: tilePoint.y,
                    column: tilePoint.x
                },
                subdomain: this._getSubdomain(tilePoint)
            };

        return this._tileFunction(view);
    },

    _loadTile: function (tile, tilePoint) {
    tile._layer = this;
    tile.onload = this._tileOnLoad;
    tile.onerror = this._tileOnError;

    this._adjustTilePoint(tilePoint);
    var tileUrl = this.getTileUrl(tilePoint);

        if (typeof tileUrl === "string") {
            tile.src = tileUrl;
        } else if (tileUrl) {
            // assume jQuery.Deferred
            tileUrl.done(function (tileUrl) {
                tile.src = tileUrl;
            });
        }
    }
});

L.tileLayer.functional = function (tileFunction, options) {
    return new L.TileLayer.Functional(tileFunction, options);
};
ismyrnow commented 11 years ago

Can you create a jsfiddle or similar to show this working? I don't have a working example to test it against.

DrYSG commented 11 years ago

First run the downloader:

http://codepen.io/DrYSG/pen/kdzft

then run this demo:

http://codepen.io/DrYSG/pen/mcdCq

ismyrnow commented 11 years ago

Took me a while, but I've merged in your changes. I tested it out with your second codepen example, and it seems to still work. I made a few other changes to support other leaflet changes.

Thanks for adding this!