如下面一个继承自TileCache的类:
- /**
- *对自定义规则切割的图片进行拼装的类
- */
- SimpleTileCache=OpenLayers.Class(OpenLayers.Layer.TileCache,{
- initialize:function(name,url,options){
- vartempoptions=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;
- },
- /**
- *按地图引擎切图规则实现的拼接方式
- */
- getURL:function(bounds){
- varres=this.map.getResolution();
- varbbox=this.map.getMaxExtent();
- varsize=this.tileSize;
- //计算列号
- vartileX=Math.round((bounds.left-bbox.left)/(res*size.w));
- //计算行号
- vartileY=Math.round((bbox.top-bounds.top)/(res*size.h));
- //当前的等级
- vartileZ=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());
- returnthis.getTilePic(tileX,tileY,tileZ);
- },
- getTilePic:function(tileX,tileY,tileZ){
- vardir='';
- if(tileZ>6){
- vardelta=Math.pow(2,tileZ-5);
- varrowDir='R'+Math.floor(tileY/delta);
- varcolDir='C'+Math.floor(tileX/delta);
- dir=tileZ+"/"+rowDir+"/"+colDir+"/";
- }else{
- dir=tileZ+'/';
- }
- vartileNo=tileZ+"-"+tileX+"-"+tileY;
- varsUrl=this.url+dir+tileNo+'.png';
- returnsUrl;
- },
- clone:function(obj){
- if(obj==null){
- obj=newSimpleTileCache(this.name,this.url,this.options);
- }
- obj=OpenLayers.Layer.TileCache.prototype.clone.apply(this,[obj]);
- returnobj;
- },
- CLASS_NAME:"SimpleTileCache"
- });
/** * 对自定义规则切割的图片进行拼装的类 */ 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; }, /** * 按地图引擎切图规则实现的拼接方式 */ getURL: function(bounds) { var res = this.map.getResolution(); var bbox = this.map.getMaxExtent(); var size = this.tileSize; //计算列号 var tileX = Math.round((bounds.left - bbox.left) / (res * size.w)); //计算行号 var tileY = Math.round((bbox.top-bounds.top) / (res * size.h)); //当前的等级 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" });
该规测实现的图片地址如下面两种形式:
当zoom>6时:
http://serverUrl.../9/R13/C26/9-418-219.png
当zoom<=6时
http://serverUrl.../4/4-12-9.png
由于到9级时切割的文件过多,再按图片切割的行Rm和列Cn存储文件。