2017-05-06 80 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這是帶有你給我的代碼的地圖,如果你在導航的右上角切換到英文,您可以看到沒有代碼的地圖,因爲它之前有錯誤的圖塊編號。 –