Openlayer 调用天地图瓦片服务

本文关键代码引用自以下博客:

http://www.3sren.com/bbs/home.php?mod=space&uid=189&do=blog&id=5026
我在此基础上根据实际应用加以定制:

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

    mapType: null,
    mirrorUrls: null,
    topLevel: null,
    bottomLevel: null,

    topLevelIndex: 0,
    bottomLevelIndex: 20,
    topTileFromX: -180,
    topTileFromY: 90,
    topTileToX: 180,
    topTileToY: -270,

    isBaseLayer: true,

    initialize: function (name,options) {

        options.topLevel = options.topLevel ? options.topLevel : this.topLevelIndex;
        options.bottomLevel = options.bottomLevel ? options.bottomLevel : this.bottomLevelIndex;
        options.maxResolution = this.getResolutionForLevel(options.topLevel);
        options.minResolution = this.getResolutionForLevel(options.bottomLevel);

        var newArguments = [name, {}, {}, options];
        OpenLayers.Layer.Grid.prototype.initialize.apply(this, newArguments);
    },

    clone: function (obj) {

        if (obj == null) {
            obj = new OpenLayers.Layer.TDTLayer(this.name, this.url, this.options);
        }

        obj = OpenLayers.Layer.Grid.prototype.clone.apply(this, [obj]);

        return obj;
    },

    getURL: function (bounds) {
        var level = this.getLevelForResolution(this.map.getResolution()); 
        var coef = 360 / Math.pow(2, level); 
        var x_num = this.topTileFromX < this.topTileToX ? Math.round((bounds.left - this.topTileFromX) / coef) : Math.round((this.topTileFromX - bounds.right) / coef);
        var y_num = this.topTileFromY < this.topTileToY ? Math.round((bounds.bottom - this.topTileFromY) / coef) : Math.round((this.topTileFromY - bounds.top) / coef);
		
		switch(level){
			case 8:
				url="http://tile6.tianditu.com/DataServer?T=tdt_vip_img_2_10_120627";
				break
			case 9:
				url="http://tile5.tianditu.com/DataServer?T=tdt_vip_img_2_10_120627";
				break
			case 10:
				url="http://tile5.tianditu.com/DataServer?T=tdt_vip_img_2_10_120627";
				break
			case 11:
				url:"http://tile1.tianditu.com/DataServer?T=E11N";
				break
			case 12:
				url="http://tile6.tianditu.com/DataServer?T=E12N";
				break
			case 13:
				url="http://tile3.tianditu.com/DataServer?T=E13N";
				break
			case 14:
				url="http://tile2.tianditu.com/DataServer?T=E14N";
				break
			case 15:
				url="http://tile5.tianditu.com/DataServer?T=tdt_vip_img_120627_dyd"
				break
			case 16:
				url="http://tile2.tianditu.com/DataServer?T=tdt_vip_img_120627_dyd";
				break
			case 17:
				url="http://tile1.tianditu.com/DataServer?T=tdt_vip_img_120627_dyd";
				break
			case 18:
				url="http://tile0.tianditu.com/DataServer?T=tdt_vip_img_120627_dyd";
				break
		}

        return this.getFullRequestString({ T: null, X: x_num, Y: y_num, L: level }, url);
    },
    selectUrl: function (a, b) { return b[a % b.length] },
    getLevelForResolution: function (res) {
        var ratio = this.getMaxResolution() / res;
        if (ratio < 1) return 0;
        for (var level = 0; ratio / 2 >= 1; )
        { level++; ratio /= 2; }
        return level;
    },
    getResolutionForLevel: function (level) {
        return 360 / 256 / Math.pow(2, level);
    },
    getMaxResolution: function () {
        return this.getResolutionForLevel(this.topLevelIndex)
    },
    getMinResolution: function () {
        return this.getResolutionForLevel(this.bottomLevelIndex)
    },
    addTile: function (bounds, position) {
        var url = this.getURL(bounds);
        return new OpenLayers.Tile.Image(this, position, bounds, url, this.tileSize);
    },

    CLASS_NAME: "OpenLayers.Layer.TiandituLayer"
});
使用非常简单,首先要引用这个js文件,然后:

var map=new OpenLayers.Map("mapDiv");
var tianditu=new OpenLayers.Layer.TiandituLayer(
		"tianditu",
		{
		    topLevel:8,
		    bottomLevel:18
		}
	);
map.addLayers([tianditu]);
完成,不过,如果天地图的服务有变动的话,还需要根据实际改动


转载于:https://my.oschina.net/LinBandit/blog/84862

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值