从目前国内的地图服务商提供的地图来看,地图瓦片切图规则只少分为三种。其中Google Map、Bing地图、MapABC、QQ地图是一类,而百度地图、搜狗地图是使用的另一类,他们是在GCJ02 基础上又进行了自己的偏移 ,还有一类应该是mapbar的地图(还没有仔细去分析它的瓦片编号规则)。
经过分析 搜狗地图瓦片编号是以0,0开始编号,向东X正增加,向西X负增加,向北Y增加,向南Y负增加,而谷歌地图是从左上角开始编号,向东X增加,向南Y增加,这种计算很方便。
知道了这种对应规则后,我们就可以得到计算的方式,代码如下:
其实还是重写getURL
1. 我们在lib\OpenLayers\Layer目录下面新建一共SoGou.js
我们的目的是取得瓦片数据的url,所以只要找到搜狗地图的瓦片url就可以了
新建一共OpenLayers.Layer.SoGou类继承自OpenLayers.Layer.TileCache ,重新定义getURL方法
/** * 对自定义规则切割的图片进行拼装的类 */ OpenLayers.Layer.SoGou = 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 = 0;},/*** 按地图引擎切图规则实现的拼接方式*/getURL: function (bounds) {var tilez=this.map.zoom-1;var offsetX = Math.pow(2,tilez); var offsetY = offsetX - 1; var res = this.map.getResolution();var bbox = this.map.getMaxExtent();var size = this.tileSize;var bx = Math.round((bounds.left - bbox.left) / (res * size.w));var by = Math.round((bbox.top - bounds.top) / (res * size.h));var numX = bx - offsetX; var numY = (-by) + offsetY; tilez = tilez + 1; var zoomLevel = 729 - tilez;if (zoomLevel == 710) zoomLevel = 792;var blo = Math.floor(numX / 200); var bla = Math.floor(numY / 200); var blos,blas; if (blo < 0) blos = "M" + ( - blo); else blos = "" + blo; if (bla < 0) blas = "M" + ( - bla); else blas = "" + bla; var x = numX.toString().replace("-","M"); var y = numY.toString().replace("-","M"); var urlsNum = parseInt((bx + by) % this.url.length); var strURL = ""; strURL = this.url[urlsNum] + zoomLevel + "/" + blos + "/" + blas + "/" + x + "_" + y + ".GIF";return strURL;},clone: function (obj) {if (obj == null) {obj = new OpenLayers.Layer.SoGou(this.name, this.url, this.options);}obj = OpenLayers.Layer.TileCache.prototype.clone.apply(this, [obj]);return obj;},CLASS_NAME: "OpenLayers.Layer.SoGou" });
请求的切片地址
var soGou=new OpenLayers.Layer.SoGou("SoGou地图",
[ "http://p0.go2map.com/seamless1/0/174/", "http://p1.go2map.com/seamless1/0/174/", "http://p2.go2map.com/seamless1/0/174/", "http://p3.go2map.com/seamless1/0/174/"],
layerOptions
);
更多精彩内容 访问OpenLayers中文官方站
(http://www.openlayers.cn)