Implement non-square tiles for L.GridLayer, see #3570

This commit is contained in:
Iván Sánchez Ortega 2015-06-30 13:54:56 +02:00
parent 7e45690169
commit cf5111306f
3 changed files with 30 additions and 18 deletions

View File

@ -37,7 +37,8 @@
var map = L.map('map', mapopts); var map = L.map('map', mapopts);
var grid = L.gridLayer({ var grid = L.gridLayer({
attribution: 'Grid Layer' attribution: 'Grid Layer',
tileSize: L.point(150, 80)
}); });
grid.createTile = function (coords, done) { grid.createTile = function (coords, done) {

View File

@ -55,6 +55,14 @@ L.Point.prototype = {
return this; return this;
}, },
dotProduct: function(point) {
return new L.Point(this.x * point.x, this.y * point.y);
},
dotDivision: function(point) {
return new L.Point(this.x / point.x, this.y / point.y);
},
round: function () { round: function () {
return this.clone()._round(); return this.clone()._round();
}, },

View File

@ -122,6 +122,11 @@ L.GridLayer = L.Layer.extend({
return document.createElement('div'); return document.createElement('div');
}, },
getTileSize: function () {
var s = this.options.tileSize;
return s instanceof L.Point ? s : new L.Point(s, s);
},
_updateZIndex: function () { _updateZIndex: function () {
if (this._container && this.options.zIndex !== undefined && this.options.zIndex !== null) { if (this._container && this.options.zIndex !== undefined && this.options.zIndex !== null) {
this._container.style.zIndex = this.options.zIndex; this._container.style.zIndex = this.options.zIndex;
@ -384,7 +389,7 @@ L.GridLayer = L.Layer.extend({
_resetGrid: function () { _resetGrid: function () {
var map = this._map, var map = this._map,
crs = map.options.crs, crs = map.options.crs,
tileSize = this._tileSize = this._getTileSize(), tileSize = this._tileSize = this.getTileSize(),
tileZoom = this._tileZoom; tileZoom = this._tileZoom;
var bounds = this._map.getPixelWorldBounds(this._tileZoom); var bounds = this._map.getPixelWorldBounds(this._tileZoom);
@ -393,19 +398,15 @@ L.GridLayer = L.Layer.extend({
} }
this._wrapX = crs.wrapLng && [ this._wrapX = crs.wrapLng && [
Math.floor(map.project([0, crs.wrapLng[0]], tileZoom).x / tileSize), Math.floor(map.project([0, crs.wrapLng[0]], tileZoom).x / tileSize.x),
Math.ceil(map.project([0, crs.wrapLng[1]], tileZoom).x / tileSize) Math.ceil(map.project([0, crs.wrapLng[1]], tileZoom).x / tileSize.y)
]; ];
this._wrapY = crs.wrapLat && [ this._wrapY = crs.wrapLat && [
Math.floor(map.project([crs.wrapLat[0], 0], tileZoom).y / tileSize), Math.floor(map.project([crs.wrapLat[0], 0], tileZoom).y / tileSize.x),
Math.ceil(map.project([crs.wrapLat[1], 0], tileZoom).y / tileSize) Math.ceil(map.project([crs.wrapLat[1], 0], tileZoom).y / tileSize.y)
]; ];
}, },
_getTileSize: function () {
return this.options.tileSize;
},
_onMoveEnd: function () { _onMoveEnd: function () {
if (!this._map) { return; } if (!this._map) { return; }
@ -510,10 +511,10 @@ L.GridLayer = L.Layer.extend({
_tileCoordsToBounds: function (coords) { _tileCoordsToBounds: function (coords) {
var map = this._map, var map = this._map,
tileSize = this._getTileSize(), tileSize = this.getTileSize(),
nwPoint = coords.multiplyBy(tileSize), nwPoint = coords.dotProduct(tileSize),
sePoint = nwPoint.add([tileSize, tileSize]), sePoint = nwPoint.add(tileSize),
nw = map.wrapLatLng(map.unproject(nwPoint, coords.z)), nw = map.wrapLatLng(map.unproject(nwPoint, coords.z)),
se = map.wrapLatLng(map.unproject(sePoint, coords.z)); se = map.wrapLatLng(map.unproject(sePoint, coords.z));
@ -551,8 +552,9 @@ L.GridLayer = L.Layer.extend({
_initTile: function (tile) { _initTile: function (tile) {
L.DomUtil.addClass(tile, 'leaflet-tile'); L.DomUtil.addClass(tile, 'leaflet-tile');
tile.style.width = this._tileSize + 'px'; var tileSize = this.getTileSize();
tile.style.height = this._tileSize + 'px'; tile.style.width = tileSize.x + 'px';
tile.style.height = tileSize.y + 'px';
tile.onselectstart = L.Util.falseFn; tile.onselectstart = L.Util.falseFn;
tile.onmousemove = L.Util.falseFn; tile.onmousemove = L.Util.falseFn;
@ -642,7 +644,7 @@ L.GridLayer = L.Layer.extend({
}, },
_getTilePos: function (coords) { _getTilePos: function (coords) {
return coords.multiplyBy(this._tileSize).subtract(this._level.origin); return coords.dotProduct(this.getTileSize()).subtract(this._level.origin);
}, },
_wrapCoords: function (coords) { _wrapCoords: function (coords) {
@ -654,9 +656,10 @@ L.GridLayer = L.Layer.extend({
}, },
_pxBoundsToTileRange: function (bounds) { _pxBoundsToTileRange: function (bounds) {
var tileSize = this.getTileSize();
return new L.Bounds( return new L.Bounds(
bounds.min.divideBy(this._tileSize).floor(), bounds.min.dotDivision(tileSize).floor(),
bounds.max.divideBy(this._tileSize).ceil().subtract([1, 1])); bounds.max.dotDivision(tileSize).ceil().subtract([1, 1]));
}, },
_noTilesToLoad: function () { _noTilesToLoad: function () {