- 論壇徽章:
- 0
|
本文系轉(zhuǎn)載他人文章 Openlayers使用TileCache對(duì)象加載預(yù)切割的圖片。每張圖片一張瓦片;其中的getURL(bound)返回的就是我們需要實(shí)現(xiàn)的圖片地
址;所以實(shí)現(xiàn)圖片地址計(jì)算算法在該函數(shù)實(shí)現(xiàn);參數(shù)bound就是一張圖片的坐標(biāo)邊界值。我們需要從這個(gè)bound計(jì)算圖片的順序數(shù)。一般地圖圖片先按等級(jí)
zoom存放,每個(gè)zoom下面為該zoom下的所有圖片,圖片過(guò)多時(shí)還可以按row值分幾個(gè)文件;如此類推。
如下面一個(gè)繼承自TileCache的類:
/**
* 對(duì)自定義規(guī)則切割的圖片進(jìn)行拼裝的類
*/
SimpleTileCache=OpenLayers.Class(OpenLayers.Layer.TileCache,{
initialize:function(name,url,options){
var tempoptions = OpenLayers.Util.extend(
{'format': 'image/png',isBaseLayer:true},options);
OpenLayers.Layer.TileCache.prototype.initialize.apply(this,[name, url, {}, tempoptions]);
this.extension = this.format.split('/')[1].toLowerCase();
this.extension = (this.extension == 'jpg') ? 'jpeg' : this.extension;
this.transitionEffect="resize";
this.buffer=2;
},
/**
* 按地圖引擎切圖規(guī)則實(shí)現(xiàn)的拼接方式
*/
getURL: function(bounds) {
var res = this.map.getResolution();
var bbox = this.map.getMaxExtent();
var size = this.tileSize;
//計(jì)算列號(hào)
var tileX = Math.round((bounds.left - bbox.left) / (res * size.w));
//計(jì)算行號(hào)
var tileY = Math.round((bbox.top-bounds.top) / (res * size.h));
//當(dāng)前的等級(jí)
var tileZ = this.map.zoom;
if(tileX<0) tileX=tileX+Math.round(bbox.getWidth()/bounds.getWidth());
if(tileY<0) tileY=tileY+Math.round(bbox.getHeight()/bounds.getHeight());
return this.getTilePic(tileX,tileY,tileZ);
},
getTilePic: function(tileX,tileY,tileZ){
var dir = '';
if(tileZ > 6) {
var delta = Math.pow(2,tileZ-5);
var rowDir = 'R'+ Math.floor(tileY /delta);
var colDir = 'C'+Math.floor(tileX /delta);
dir = tileZ + "/" + rowDir + "/" + colDir + "/";
} else {
dir= tileZ + '/';
}
var tileNo = tileZ + "-" + tileX + "-" + tileY;
var sUrl = this.url + dir + tileNo + '.png';
return sUrl;
},
clone: function (obj) {
if (obj == null) {
obj = new SimpleTileCache(this.name,this.url,this.options);
}
obj = OpenLayers.Layer.TileCache.prototype.clone.apply(this, [obj]);
return obj;
},
CLASS_NAME: "SimpleTileCache"
});
該規(guī)測(cè)實(shí)現(xiàn)的圖片地址如下面兩種形式:
當(dāng)zoom>6時(shí):
http://serverUrl.../9/R13/C26/9-418-219.png
當(dāng)zoom<=6時(shí)
http://serverUrl.../4/4-12-9.png
由于到9級(jí)時(shí)切割的文件過(guò)多,再按圖片切割的行Rm和列Cn存儲(chǔ)文件。
|
|