Tianditu.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /**
  2. * @requires SuperMap/Layer/CanvasLayer.js
  3. * @requires SuperMap/Layer/Grid.js
  4. * @requires SuperMap/Tile/Image.js
  5. */
  6. /**
  7. * Class: SuperMap.Layer.Tianditu
  8. * 天地图服务图层类。
  9. * 用于显示天地图的地图服务,使用<SuperMap.Layer.Tianditu>的
  10. * 构造函数可以创建天地图图层,更多信息查看:
  11. *
  12. * Inherits from:
  13. * - <SuperMap.Layer.CanvasLayer>
  14. */
  15. SuperMap.Layer.Tianditu = SuperMap.Class(SuperMap.CanvasLayer, {
  16. /**
  17. * APIProperty: layerType
  18. * {String} 图层类型.(vec:矢量图层,img:影像图层,ter:地形图层)
  19. */
  20. layerType:"vec", //(vec:矢量图层,cva:矢量标签图层,img:影像图层,cia:影像标签图层,ter:地形,cta:地形标签图层)
  21. /**
  22. * APIProperty: isLabel
  23. * {Boolean} 是否是标签图层.
  24. */
  25. isLabel:false,
  26. /**
  27. * Property: attribution
  28. * {String} The layer attribution.
  29. */
  30. attribution: "Data by <a style='white-space: nowrap' target='_blank' href='http://www.tianditu.com'>Tianditu</a>",
  31. /**
  32. * Property: url
  33. * {String} 图片url.
  34. */
  35. url:"http://t${num}.tianditu.com/DataServer?T=${type}_${proj}&x=${x}&y=${y}&l=${z}",
  36. //cva_url:"http://t${num}.tianditu.com/DataServer?T=cva_${proj}&x=${x}&y=${y}&l=${z}",
  37. /**
  38. * Property: zOffset
  39. * {Number} 图片url中z值偏移量
  40. */
  41. zOffset:1,
  42. /**
  43. * APIProperty: dpi
  44. * {Float} 屏幕上每英寸包含像素点的个数。
  45. * 该参数结合图层比例尺可以推算出该比例尺下图层的分辨率.默认为96。
  46. */
  47. dpi: 96,
  48. /**
  49. * Constructor: SuperMap.Layer.Tianditu
  50. * 创建天地图图层
  51. *
  52. * Example:
  53. * (code)
  54. * var tiandituLayer = new SuperMap.Layer.Tianditu();
  55. * (end)
  56. */
  57. initialize: function (options) {
  58. var me = this;
  59. me.name = "Tianditu";
  60. // options = SuperMap.Util.extend({
  61. // maxExtent: new SuperMap.Bounds(
  62. // minX, minY, maxX, maxY
  63. // ),
  64. // tileOrigin:new SuperMap.LonLat(minX, maxY),
  65. // //maxResolution:maxResolution,
  66. // //minResolution:minResolution,
  67. // resolutions:resolutions,
  68. // units:me.units,
  69. // projection:me.projection
  70. // }, options);
  71. SuperMap.CanvasLayer.prototype.initialize.apply(me, [me.name, me.url, null, options]);
  72. },
  73. /**
  74. * APIMethod: clone
  75. * 创建当前图层的副本。
  76. *
  77. * Parameters:
  78. * obj - {Object}
  79. *
  80. * Returns:
  81. * {<SuperMap.Layer.Tianditu>} 新的图层。
  82. */
  83. clone: function (obj) {
  84. var me = this;
  85. if (obj == null) {
  86. obj = new SuperMap.Layer.Tianditu(
  87. me.name, me.url, me.params, me.getOptions());
  88. }
  89. obj = SuperMap.CanvasLayer.prototype.clone.apply(me, [obj]);
  90. obj._timeoutId = null;
  91. return obj;
  92. },
  93. /**
  94. * Method: getTileUrl
  95. * 获取每个tile的图片url
  96. *
  97. * Parameters:
  98. * xyz - {Object}
  99. */
  100. getTileUrl:function(xyz){
  101. var me = this;
  102. url = me.url;
  103. var proj = this.projection;
  104. if(proj.getCode){
  105. proj = proj.getCode();
  106. }
  107. if(proj=="EPSG:4326"){
  108. var proj = "c"
  109. }
  110. else{
  111. var proj = "w";
  112. }
  113. var x = xyz.x;
  114. var y = xyz.y;
  115. var z = xyz.z+me.zOffset;
  116. var num = Math.abs((xyz.x + xyz.y) % 7);
  117. var lt = this.layerType;
  118. if(this.isLabel){
  119. if(this.layerType=="vec")lt="cva"
  120. if(this.layerType=="img")lt="cia"
  121. if(this.layerType=="ter")lt="cta"
  122. }
  123. url = SuperMap.String.format(url, {
  124. num: num,
  125. x: x,
  126. y: y,
  127. z: z,
  128. proj:proj,
  129. type:lt
  130. });
  131. return url;
  132. },
  133. /**
  134. * Method: setMap
  135. * Set the map property for the layer. This is done through an accessor
  136. * so that subclasses can override this and take special action once
  137. * they have their map variable set.
  138. *
  139. * Here we take care to bring over any of the necessary default
  140. * properties from the map.
  141. *
  142. * Parameters:
  143. * map - {<SuperMap.Map>}
  144. */
  145. setMap: function(map) {
  146. SuperMap.CanvasLayer.prototype.setMap.apply(this, [map]);
  147. var proCode = null;
  148. var proj = this.projection||map.projection;
  149. if(proj){
  150. if(proj.getCode){
  151. proCode = proj.getCode();
  152. }
  153. else{
  154. proCode = proj;
  155. }
  156. }
  157. this.setTiandituParam(proCode);
  158. },
  159. /**
  160. * Method: setTiandituParam
  161. * 设置出图相关参数
  162. *
  163. * Parameters:
  164. * projection - {String} 投影坐标系
  165. */
  166. setTiandituParam:function(projection){
  167. var lt = this.layerType;
  168. if(lt=="vec"){
  169. var resLen = 17;
  170. var resStart = 0;
  171. this.zOffset = 1;
  172. this.numZoomLevels = 17;
  173. }
  174. else if(lt=="img"){
  175. var resLen = 17;
  176. var resStart = 0;
  177. this.zOffset = 1;
  178. this.numZoomLevels = 17;
  179. }
  180. else if(lt=="ter"){
  181. var resLen = 13;
  182. var resStart = 0;
  183. this.zOffset = 1;
  184. this.numZoomLevels = 13;
  185. }
  186. if(projection=="EPSG:4326"){
  187. var minX = -180;
  188. var minY = -90;
  189. var maxX= 180;
  190. var maxY= 90;
  191. //var maxResolution = 156543.0339;
  192. //var minResolution = 0.5971642833709717;
  193. var resolutions = [];
  194. for(var i=resStart;i<resLen;i++){
  195. resolutions.push(1.40625/2/Math.pow(2,i));
  196. }
  197. this.units = "degree";
  198. this.projection = new SuperMap.Projection("EPSG:4326");
  199. this.maxExtent=new SuperMap.Bounds(
  200. minX, minY, maxX, maxY
  201. );
  202. this.tileOrigin = new SuperMap.LonLat(minX, maxY);
  203. this.resolutions = resolutions;
  204. }
  205. else{
  206. var minX = -20037508.3392;
  207. var minY = -20037508.3392;
  208. var maxX= 20037508.3392;
  209. var maxY= 20037508.3392;
  210. //var maxResolution = 156543.0339;
  211. //var minResolution = 0.5971642833709717;
  212. var resolutions = [];
  213. for(var i=resStart;i<resLen;i++){
  214. resolutions.push(156543.0339/2/Math.pow(2,i));
  215. }
  216. //this.numZoomLevels = 18;
  217. this.units = "m";
  218. this.projection = new SuperMap.Projection("EPSG:3857");
  219. this.maxExtent=new SuperMap.Bounds(
  220. minX, minY, maxX, maxY
  221. );
  222. this.tileOrigin = new SuperMap.LonLat(minX, maxY);
  223. this.resolutions = resolutions;
  224. }
  225. },
  226. CLASS_NAME: 'SuperMap.Layer.Tianditu'
  227. });