OSM.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * @requires SuperMap/Util.js
  3. * @requires SuperMap/Layer/CanvasLayer.js
  4. */
  5. /**
  6. * Class: SuperMap.Layer.OSM
  7. * 此图层可以访问OpenStreetMap的地图服务。
  8. *
  9. * Inherits from:
  10. * - <SuperMap.Layer.CanvasLayer>
  11. */
  12. SuperMap.Layer.OSM = SuperMap.Class(SuperMap.CanvasLayer, {
  13. /**
  14. * APIProperty: name
  15. * {String}图层名称,默认为“OpenStreetMap”,防止初始化时未设置图层名
  16. *
  17. */
  18. name: "OpenStreetMap",
  19. /**
  20. * Property: url
  21. * {String}默认的OpenStreetMap的三个服务器地址,不需要要用户设置
  22. */
  23. url: [
  24. 'http://a.tile.openstreetmap.org/${z}/${x}/${y}.png',
  25. 'http://b.tile.openstreetmap.org/${z}/${x}/${y}.png',
  26. 'http://c.tile.openstreetmap.org/${z}/${x}/${y}.png'
  27. ],
  28. /**
  29. * Property: attribution
  30. * {String} The layer attribution.
  31. */
  32. attribution: "Data CC-By-SA by <a style='white-space: nowrap' target='_blank' href='http://openstreetmap.org/'>OpenStreetMap</a>",
  33. /**
  34. * Constructor: SuperMap.Layer.OSM
  35. * 创建OSM图层,可以浏览OpenStreetMap地图
  36. * Example:
  37. * (code)
  38. *
  39. * var osm = new SuperMap.Layer.OSM("MyName");
  40. *
  41. * (end)
  42. *
  43. * 默认为墨卡托投影,所以当需要地图定位以及添加元素在地图上时都需要坐标转换
  44. * Example:
  45. * (code)
  46. *
  47. * var markers = new SuperMap.Layer.Markers( "Markers" );
  48. * map.addLayer(markers);
  49. * var size = new SuperMap.Size(21,25);
  50. * var offset = new SuperMap.Pixel(-(size.w/2), -size.h);
  51. * var icon = new SuperMap.Icon('图片地址', size, offset);
  52. * markers.addMarker(new SuperMap.Marker(new SuperMap.LonLat(118,40 ).transform(
  53. * new SuperMap.Projection("EPSG:4326"),
  54. * map.getProjectionObject()),icon));
  55. *
  56. * (end)
  57. * Parameters:
  58. * name - {String} 图层名称
  59. */
  60. initialize: function(name, options) {
  61. options = SuperMap.Util.extend({
  62. projection: "EPSG:3857",
  63. numZoomLevels: 20
  64. }, options);
  65. SuperMap.CanvasLayer.prototype.initialize.apply(this,[name,this.url,{},options] );
  66. },
  67. /**
  68. * Method: clone
  69. */
  70. clone: function(obj) {
  71. if (obj == null) {
  72. obj = new SuperMap.Layer.OSM(
  73. this.name, this.url, this.getOptions());
  74. }
  75. obj = SuperMap.CanvasLayer.prototype.clone.apply(this, [obj]);
  76. return obj;
  77. },
  78. /**
  79. * APIMethod: destroy
  80. * 解构OSM类,释放资源。
  81. */
  82. destroy: function () {
  83. var me = this;
  84. SuperMap.CanvasLayer.prototype.destroy.apply(me, arguments);
  85. },
  86. /**
  87. * Method: getTileUrl
  88. * 获取瓦片的URL。
  89. *
  90. * Parameters:
  91. * xyz - {Object} 一组键值对,表示瓦片X, Y, Z方向上的索引。
  92. *
  93. * Returns
  94. * {String} 瓦片的 URL 。
  95. */
  96. getTileUrl: function (xyz) {
  97. var me = this, url;
  98. if (SuperMap.Util.isArray(this.url)) {
  99. url = me.selectUrl(xyz, this.url);
  100. }
  101. url= SuperMap.String.format(url, {
  102. x: xyz.x,
  103. y: xyz.y,
  104. z: xyz.z
  105. });
  106. return url;
  107. },
  108. /**
  109. * Method: selectUrl
  110. * 通过某种方式实现在一组url数组中选出合理的url
  111. * Parameters:
  112. * xyz - {Object} 一组键值对,表示瓦片X, Y, Z方向上的索引。
  113. * urls - {Array(String)} url数组
  114. *
  115. * Returns:
  116. * {String} 一个合理的url,主要用于出图访问多个服务器,提高效率
  117. */
  118. selectUrl: function(xyz, urls) {
  119. var id=Math.abs(xyz.x+xyz.y)%urls.length;
  120. var url=urls[id];
  121. return url;
  122. },
  123. CLASS_NAME: "SuperMap.Layer.OSM"
  124. });