2017-05-06 87 views
1

我正在使用传单的自定义贴图。到目前为止,一切正常,但不幸的是,我用来将我的图像分割成瓦片的程序不是以0开始计数,而是以1开始,所以我的瓦片以“1_1.jpg”开头,所以我的整个地图都移动了一个瓦片在y轴和x轴上。要重命名的瓷砖是不是一种选择辩论,因为还有很多,所以我一直在寻找一个可能改变传单自定义网址自定义贴图

L.tileLayer('images/map/{z}/C{x}_R{y}.jpg', 

喜欢的东西x=x+1y=y+1的{X}和{Y}值,这将是我的逻辑。 我读过它可能与getTileUrl,但我不明白如何。我对JavaScript仍然很陌生,这个问题开始让我生气!

如果有人能帮助,我会非常感激。

瓷砖名称就像“Cx_Ry.jpg”(例如第一张图片“C1_R1.jpg”)这里是代码。

var w = 16384, h = 16384; //Größe innerhalb Box 

var map = L.map('image-map', { 
     minZoom: 0, 
     maxZoom: 5, 
     crs: L.CRS.Simple, 
     attributionControl: false, 
}).setView([0, 0], 0); 

var southWest = map.unproject([0, h], map.getMaxZoom()); 
var northEast = map.unproject([w, 0], map.getMaxZoom()); 
var bounds = new L.LatLngBounds(southWest, northEast); 

map.setMaxBounds(bounds); 

L.tileLayer('images/map/{z}/C{x}_R{y}.jpg', { 
    minZoom: 0, 
    maxZoom: 5, 
    tms: false, 
    continuousWorld: 'false', 
    noWrap: false, 
    defaultRadius:1, 
}).addTo(map); 

回答

0

可以扩展传单的TileLayer类来提供自己的getTileUrl方法:http://leafletjs.com/examples/extending/extending-2-layers.html

在这种情况下,它可能会是这个样子:

L.TileLayer.MyCustomLayer = L.TileLayer.extend({ 
    getTileUrl: function(coords) { 
     // increment our x/y coords by 1 so they match our tile naming scheme 
     coords.x = coords.x + 1; 
     coords.y = coords.y + 1; 

     // pass the new coords on through the original getTileUrl 
     // see http://leafletjs.com/examples/extending/extending-1-classes.html 
     // for calling parent methods 
     return L.TileLayer.prototype.getTileUrl.call(this, coords); 
    } 
}); 

// static factory as recommended by http://leafletjs.com/reference-1.0.3.html#class 
L.tileLayer.myCustomLayer = function(templateUrl, options) { 
    return new L.TileLayer.MyCustomLayer(templateUrl, options); 
} 

// create the layer and add it to the map 
L.tileLayer.myCustomLayer('images/map/{z}/C{x}_R{y}.jpg', { 
    minZoom: 0, 
    maxZoom: 5, 
    tms: false, 
    continuousWorld: 'false', 
    noWrap: false, 
    defaultRadius:1, 
}).addTo(map); 

代码是未经测试,但应该让你在正确的方向前进。

+0

非常感谢,这肯定会朝着我想要的正确方向发展,不幸的是,所发生的一切是我的地图现在完全消失,我不知道为什么:/ –

+0

控制台中的任何错误?如果您查看网络选项卡,是否请求正确的磁贴? –

+0

不是我会注意到......但实际上你可以看看这里的所有东西:http://orboroth.bplaced.net/karte.html这是带有你给我的代码的地图,如果你在导航的右上角切换到英文,您可以看到没有代码的地图,因为它之前有错误的图块编号。 –