tileLayer.wmts.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. L.TileLayer.WMTS = L.TileLayer.extend({
  2. defaultWmtsParams: {
  3. service: 'WMTS',
  4. request: 'GetTile',
  5. version: '1.0.0',
  6. layers: '',
  7. styles: '',
  8. tilematrixSet: '',
  9. format: 'image/jpeg'
  10. },
  11. initialize: function (url, options) { // (String, Object)
  12. this._url = url;
  13. var wmtsParams = L.extend({}, this.defaultWmtsParams);
  14. var tileSize = options.tileSize || this.options.tileSize;
  15. if (options.detectRetina && L.Browser.retina) {
  16. wmtsParams.width = wmtsParams.height = tileSize * 2;
  17. } else {
  18. wmtsParams.width = wmtsParams.height = tileSize;
  19. }
  20. for (var i in options) {
  21. // all keys that are not TileLayer options go to WMTS params
  22. if (!this.options.hasOwnProperty(i) && i!="matrixIds" && i!= "jsonUrl") {
  23. wmtsParams[i] = options[i];
  24. }
  25. }
  26. this.wmtsParams = wmtsParams;
  27. this.matrixIds = options.matrixIds||this.getDefaultMatrix();
  28. L.setOptions(this, options);
  29. },
  30. onAdd: function (map) {
  31. this._crs = this.options.crs || map.options.crs;
  32. L.TileLayer.prototype.onAdd.call(this, map);
  33. },
  34. getParamString: function (obj, existingUrl, uppercase) {
  35. var params = [];
  36. for (var i in obj) {
  37. if (i == 'secret')
  38. continue;
  39. params.push(encodeURIComponent(uppercase ? i.toUpperCase() : i) + '=' + encodeURIComponent(obj[i]));
  40. }
  41. return ((!existingUrl || existingUrl.indexOf('?') === -1) ? '?' : '&') + params.join('&');
  42. },
  43. getTileUrl: function (coords) { // (Point, Number) -> String
  44. var url = L.Util.template(this._url, {s: this._getSubdomain(coords)});
  45. //argis版本的要-1
  46. url += this.getParamString(this.wmtsParams, url) + "&tilematrix=" + (coords.z) + "&tilerow=" + coords.y +"&tilecol=" + coords.x;
  47. if (this.wmtsParams.hnjhpt_rid) {
  48. var date = new Date();
  49. var time = date.getTime();
  50. var message = this.wmtsParams.hnjhpt_sid + this.wmtsParams.hnjhpt_rid + time;
  51. var hash = CryptoJS.HmacSHA256(message, this.wmtsParams.secret);
  52. var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
  53. url += "&hnjhpt_sign=" + encodeURIComponent(hashInBase64) + "&hnjhpt_rtime=" + time
  54. }
  55. return url;
  56. },
  57. setParams: function (params, noRedraw) {
  58. L.extend(this.wmtsParams, params);
  59. if (!noRedraw) {
  60. this.redraw();
  61. }
  62. return this;
  63. },
  64. getDefaultMatrix : function () {
  65. /**
  66. * the matrix3857 represents the projection
  67. * for in the IGN WMTS for the google coordinates.
  68. */
  69. var matrixIds3857 = new Array(22);
  70. for (var i= 0; i<22; i++) {
  71. matrixIds3857[i]= {
  72. identifier : "" + i,
  73. topLeftCorner : new L.LatLng(20037508.3428,-20037508.3428)
  74. };
  75. }
  76. return matrixIds3857;
  77. }
  78. });
  79. L.tileLayer.wmts = function (url, options) {
  80. return new L.TileLayer.WMTS(url, options);
  81. };