OpenLayers源码学习21:XYZ

看完WMTS之后, 发现XYZ这个类倒是最简单, 其实应该从XYZ这开始看

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
OpenLayers.Layer.XYZ = OpenLayers.Class(OpenLayers.Layer.Grid, {
isBaseLayer: true,
//墨卡托投影
sphericalMercator: false,
//zoom全局偏移
zoomOffset: 0,
// 服务端分辨率列表
serverResolutions: null,
// 构造
initialize: function(name, url, options) {
if (options && options.sphericalMercator || this.sphericalMercator) {
options = OpenLayers.Util.extend({//有墨卡托投影, 转成900913_19级
projection: "EPSG:900913",
numZoomLevels: 19
}, options);
}
OpenLayers.Layer.Grid.prototype.initialize.apply(this, [
name || this.name, url || this.url, {}, options
]);
},
//克隆
clone: function (obj) {
if (obj == null) {
obj = new OpenLayers.Layer.XYZ(this.name,
this.url,
this.getOptions());
}
//父类构造
obj = OpenLayers.Layer.Grid.prototype.clone.apply(this, [obj]);
return obj;
},
// 给瓦片用的接口, 产生实际url
getURL: function (bounds) {
var xyz = this.getXYZ(bounds);
var url = this.url;
if (OpenLayers.Util.isArray(url)) {
var s = '' + xyz.x + xyz.y + xyz.z;
url = this.selectUrl(s, url);
}
return OpenLayers.String.format(url, xyz);
},
// 瓦片位置计算公式
getXYZ: function(bounds) {
var res = this.getServerResolution();
var x = Math.round((bounds.left - this.maxExtent.left) /
(res * this.tileSize.w));// (传入bound - 地图边界最左)/(分辨率x瓦片宽)
var y = Math.round((this.maxExtent.top - bounds.top) /
(res * this.tileSize.h));
var z = this.getServerZoom();
if (this.wrapDateLine) {
var limit = Math.pow(2, z);
x = ((x % limit) + limit) % limit;
}
return {'x': x, 'y': y, 'z': z};
},
// 设置持有的map对象
setMap: function(map) {
OpenLayers.Layer.Grid.prototype.setMap.apply(this, arguments);
if (!this.tileOrigin) {
this.tileOrigin = new OpenLayers.LonLat(this.maxExtent.left,
this.maxExtent.bottom);
}
},
CLASS_NAME: "OpenLayers.Layer.XYZ"
});