openlayers加载天地图代码

基于openlayers扩展,加载天地图数据源。
首先是TDTLayer.js文件

OpenLayers.Layer.TDTServer = [
            "http://tile0.tianditu.com/DataServer?",
            "http://tile1.tianditu.com/DataServer?",
            "http://tile2.tianditu.com/DataServer?",
            "http://tile3.tianditu.com/DataServer?",
            "http://tile4.tianditu.com/DataServer?",
            "http://tile5.tianditu.com/DataServer?",
            "http://tile6.tianditu.com/DataServer?",
            "http://tile7.tianditu.com/DataServer?"
];

OpenLayers.Layer.TDTLayer = OpenLayers.Class(OpenLayers.Layer.Grid, {

    isBaseLayer: true,

    zoomOffset: 2, //0.3515625,

    //maxResolution: 0.3515625,
    //maxResolution: 360 / 256,

    format: 'image/png',

    initialize: function(name, url, layername, options) {
        this.layername = layername;
        OpenLayers.Layer.Grid.prototype.initialize.apply(this, [name, url, {}, options]); 
    },    

    clone: function (obj) {        
        if (obj == null) {
            obj = new OpenLayers.Layer.TDTLayer(this.name,
                                                 this.url,
                                                 this.layername,
                                                 this.getOptions());
        } 
        obj = OpenLayers.Layer.Grid.prototype.clone.apply(this, [obj]); 
        return obj;
    },    

    getLayerName: function(tileZ){
        return this.layername;
    },    

    getURL: function(bounds) {
        var res = this.map.getResolution();

        var tileX = Math.round((bounds.left - this.maxExtent.left) 
            / (res * this.tileSize.w));
        var tileY = Math.round((this.maxExtent.top - bounds.top) 
            / (res * this.tileSize.h));
        var tileZ = this.map.getZoom() + this.zoomOffset;

        var path = OpenLayers.String.format("T=${T}&X=${X}&Y=${Y}&L=${L}", {'T': this.getLayerName(tileZ),'X': tileX, 'Y': tileY, 'L': tileZ});
        var url = this.url;
        if (url instanceof Array) {
            url = this.selectUrl(path, url);
        }
        return url + path;
    },

    addTile:function(bounds, position) {
        var url = this.getURL(bounds);
        return new OpenLayers.Tile.Image(this, position, bounds, 
                                             url, this.tileSize);
    },

    CLASS_NAME: "OpenLayers.Layer.TDTLayer"
});

function calcResolutions (lvstart, lvend){
    var maxResolution = 360 / 256;
    var res = new Array();
    for(var i=lvstart;  i<=lvend; i++){
        res.push( maxResolution / Math.pow( 2, i));
    }
    return res;
};
/*
天地图全球遥感数据
*/
OpenLayers.Layer.TDTRSLayer = OpenLayers.Class(OpenLayers.Layer.TDTLayer, {
        labelLayer: null,

    initialize: function(name, options) {   
//      var levels = 18 || options.levels; 
        options = OpenLayers.Util.extend({
            projection: "EPSG:4326"
//          resolutions: calcResolutions(2, levels)
        }, options);                    

        OpenLayers.Layer.TDTLayer.prototype.initialize.apply(this, [name, OpenLayers.Layer.TDTServer, null, options]); 
    },    

    setVisibility: function(visible){
          OpenLayers.Layer.TDTLayer.prototype.setVisibility.apply(this, [visible]);
          if(this.showLabel){
                this.showLabelLayer(visible);
          }       
    },

    showLabelLayer: function(visible){
            if(!this.labelLayer && this.map){
                    this.labelLayer= new OpenLayers.Layer.TDTRSDMLayer("Label",{
                                isBaseLayer : false,
                                singleTile : false ,
                                displayInLayerSwitcher: false
                        });
                        this.map.addLayer(this.labelLayer);
                }
                this.labelLayer.setVisibility(visible);
    },

    getLayerName: function(tileZ){

                if(tileZ >= 0 && tileZ <= 10){
                    return "sbsm0210";//http://tile0.tianditu.com/services/sbsm0210/GetCapabilities
                } else  if(tileZ >= 11 && tileZ <= 13){
                    return "e" + tileZ;
                    //http://tile0.tianditu.com/services/e11/GetCapabilities
                    //http://tile0.tianditu.com/services/e12/GetCapabilities
                    //http://tile0.tianditu.com/services/e12/GetCapabilities
                } else  if(tileZ == 14){
                    return "eastdawnall";
                    //http://tile0.tianditu.com/services/eastdawnall/GetCapabilities
                } else  if(tileZ >= 15 && tileZ <= 18){
                    return "sbsm1518";
                    //http://tile0.tianditu.com/services/sbsm1518/GetCapabilities
                    //return "AP0115_SWHK";
                    //http://tile0.tianditu.com/services/AP0115_SWHK/GetCapabilities
                }       

                return this.layername;
    },  

    clone: function (obj) {        
        if (obj == null) {
            obj = new OpenLayers.Layer.TDTRSLayer(this.name, this.getOptions());
        } 
        obj = OpenLayers.Layer.TDTLayer.prototype.clone.apply(this, [obj]); 
        return obj;
    },        

    CLASS_NAME: "OpenLayers.Layer.TDTRSLayer"
});

/*
天地图影像地名数据
*/
OpenLayers.Layer.TDTRSDMLayer = OpenLayers.Class(OpenLayers.Layer.TDTLayer, {
    initialize: function(name, options) {
        //var levels = 18 || options.levels; 
        options = OpenLayers.Util.extend({
            projection: "EPSG:4326"
            //resolutions: calcResolutions(2, levels)
        }, options); 
        OpenLayers.Layer.TDTLayer.prototype.initialize.apply(this, [name, OpenLayers.Layer.TDTServer, null, options]); 
    },    

    getLayerName: function(tileZ){

    //1-10
    //http://tile0.tianditu.com/services/AB0512_Anno/GetCapabilities //中文地名注记
    //1-10
    //http://tile0.tianditu.com/services/AB0106_AnnoE/GetCapabilities //英文地名注记
        if(tileZ >= 1 && tileZ <= 10){
            return "A0610_ImgAnno"; 
    //1-10
    //http://tile0.tianditu.com/services/A0610_ImgAnno/GetCapabilities //中文地名注记
    //1-10
    //http://tile0.tianditu.com/services/A0104_ImgAnnoE/GetCapabilities //英文地名注记
      } else    if(tileZ >= 11 && tileZ <= 14){
            return "B0530_eImgAnno"; 
      } else    if(tileZ >= 15 && tileZ <= 18){
            return "siweiAnno68"; 
      }   
    //11-14
    //http://tile0.tianditu.com/services/B0530_eImgAnno/GetCapabilities
    //15-18  
    //http://tile0.tianditu.com/services/siweiAnno68/GetCapabilities

        return this.layername;
    },  

    clone: function (obj) {        
        if (obj == null) {
            obj = new OpenLayers.Layer.TDTRSDMLayer(this.name, this.getOptions());
        } 
        obj = OpenLayers.Layer.TDTLayer.prototype.clone.apply(this, [obj]); 
        return obj;
    },        

    CLASS_NAME: "OpenLayers.Layer.TDTRSDMLayer"
});

/*
天地图区划地名数据
*/
OpenLayers.Layer.TDTQHDMLayer = OpenLayers.Class(OpenLayers.Layer.TDTLayer, {
    initialize: function(name, options) {
        //var levels = 10 || options.levels; 
        options = OpenLayers.Util.extend({
            projection: "EPSG:4326"
//          resolutions: calcResolutions(2, levels)
        }, options); 
        OpenLayers.Layer.TDTLayer.prototype.initialize.apply(this, [name, OpenLayers.Layer.TDTServer, null, options]); 
    },    

    getLayerName: function(tileZ){

    //1-10
    //http://tile0.tianditu.com/services/AB0512_Anno/GetCapabilities //中文地名注记
    //1-10
    //http://tile0.tianditu.com/services/AB0106_AnnoE/GetCapabilities //英文地名注记
        if(tileZ >= 1 && tileZ <= 10){
            return "A0610_ImgAnno"; 
    //1-10
    //http://tile0.tianditu.com/services/A0610_ImgAnno/GetCapabilities //中文地名注记
    //1-10
    //http://tile0.tianditu.com/services/A0104_ImgAnnoE/GetCapabilities //英文地名注记
      }/* else  if(tileZ >= 11 && tileZ <= 14){
            return "B0530_eImgAnno"; 
      } else    if(tileZ >= 15 && tileZ <= 18){
            return "siweiAnno68"; 
      }   */
    //11-14
    //http://tile0.tianditu.com/services/B0530_eImgAnno/GetCapabilities
    //15-18  
    //http://tile0.tianditu.com/services/siweiAnno68/GetCapabilities

        return this.layername;
    },  

    clone: function (obj) {        
        if (obj == null) {
            obj = new OpenLayers.Layer.TDTDMLayer(this.name, this.getOptions());
        } 
        obj = OpenLayers.Layer.TDTLayer.prototype.clone.apply(this, [obj]); 
        return obj;
    },        

    CLASS_NAME: "OpenLayers.Layer.TDTQHDMLayer"
});

/*
天地图全球区划数据
*/
OpenLayers.Layer.TDTQHLayer = OpenLayers.Class(OpenLayers.Layer.TDTLayer, {
        labelLayer: null,

    initialize: function(name, options) {
//      var levels = 18 || options.levels; 
        options = OpenLayers.Util.extend({
            projection: "EPSG:4326"
//          resolutions: calcResolutions(2, levels)
        }, options);   
        OpenLayers.Layer.TDTLayer.prototype.initialize.apply(this, [name, OpenLayers.Layer.TDTServer, null, options]); 
    },   

    setVisibility: function(visible){
          OpenLayers.Layer.TDTLayer.prototype.setVisibility.apply(this, [visible]);
          if(this.showLabel){
                this.showLabelLayer(visible);
          }       
    },

    showLabelLayer: function(visible){
            if(!this.labelLayer && this.map){
                    this.labelLayer= new OpenLayers.Layer.TDTQHDMLayer("Label",{
                                isBaseLayer : false,
                                singleTile : false ,
                                displayInLayerSwitcher: false
                        });
                        this.map.addLayer(this.labelLayer);
                }
                this.labelLayer.setVisibility(visible);
    },


    getLayerName: function(tileZ){

        if(tileZ >= 1 && tileZ <= 10){
            return "A0512_EMap";
        } else  if(tileZ >= 11 && tileZ <= 12){
            return "B0627_EMap1112"; 
        } else  if(tileZ >= 13 && tileZ <= 18){
            return "siwei0608"; 
        }       

        //1-10
        //http://tile0.tianditu.com/services/A0512_EMap/GetCapabilities
        //11-12
        //http://tile0.tianditu.com/services/B0627_EMap1112/GetCapabilities
        //13-18
        //http://tile0.tianditu.com/services/siwei0608/GetCapabilities

        return this.layername;
    },  

    clone: function (obj) {        
        if (obj == null) {
            obj = new OpenLayers.Layer.TDTQHLayer(this.name, this.getOptions());
        } 
        obj = OpenLayers.Layer.TDTLayer.prototype.clone.apply(this, [obj]); 
        return obj;
    },        

    CLASS_NAME: "OpenLayers.Layer.TDTQHLayer"
});

调用方法(可能有些废代码,但是效果能出来)

map = new OpenLayers.Map(mapdiv, mapOptions);
map.tlayer = new OpenLayers.Layer.TDTLayer("影像图",
             "http://t5.tianditu.com/DataServer?" ,'vec_c', {
                topLevel: 1,
                 bottomLevel: 18,
                 //maxExtent: (new OpenLayers.Bounds(-180, -90, 180, 90)).transform(new OpenLayers.Projection("EPSG:4326"),map.getProjectionObject()),
                mirrorUrls:["http://t0.tianditu.com/DataServer","http://t1.tianditu.com/DataServer","http://t2.tianditu.com/DataServer","http://t3.tianditu.com/DataServer","http://t4.tianditu.com/DataServer","http://t5.tianditu.com/DataServer","http://t6.tianditu.com/DataServer","http://t7.tianditu.com/DataServer"],
                isBaseLayer : true,
            });

    map.addLayer(map.tlayer);

效果如下
使用效果

猜你喜欢

转载自blog.csdn.net/atove/article/details/44617351