关键词搜索

源码搜索 ×
×

Leaflet笔记-把leaflet-tilelayer-wmts移植到vue cli中(含思路)

发布2019-09-11浏览6484次

详情内容

目录

 

 

前言

过程


 

前言

关于leaflet的webpackage使用npm安装官方是有明显的解析

但是关于插件特别是TileLayer.WMTS是不提供的,但提供了源码,可以稍微修改下,就能在vue cli中使用,在此记录下,这个经验很有用,方便自己以后查阅。

 

过程

先把leaflet下载下来

npm install leaflet

把leaflet.TileLayer.WMTS的Github中把leaflet-tilelayer-wmts-src.js的源码拷贝下!

安装好后在vue中引用:

拷贝后,把源码修改为:

  1. /***
  2. * 插件移植开始
  3. */
  4. $L.TileLayer.WMTS = $L.TileLayer.extend({
  5. defaultWmtsParams: {
  6. service: 'WMTS',
  7. request: 'GetTile',
  8. version: '1.0.0',
  9. layer: '',
  10. style: '',
  11. tilematrixset: '',
  12. format: 'image/jpeg'
  13. },
  14. initialize: function (url, options) { // (String, Object)
  15. this._url = url;
  16. var lOptions= {};
  17. var cOptions = Object.keys(options);
  18. cOptions.forEach(element=>{
  19. lOptions[element.toLowerCase()]=options[element];
  20. });
  21. var wmtsParams = $L.extend({}, this.defaultWmtsParams);
  22. var tileSize = lOptions.tileSize || this.options.tileSize;
  23. if (lOptions.detectRetina && $L.Browser.retina) {
  24. wmtsParams.width = wmtsParams.height = tileSize * 2;
  25. } else {
  26. wmtsParams.width = wmtsParams.height = tileSize;
  27. }
  28. for (var i in lOptions) {
  29. // all keys that are in defaultWmtsParams options go to WMTS params
  30. if (wmtsParams.hasOwnProperty(i) && i!="matrixIds") {
  31. wmtsParams[i] = lOptions[i];
  32. }
  33. }
  34. this.wmtsParams = wmtsParams;
  35. this.matrixIds = options.matrixIds||this.getDefaultMatrix();
  36. $L.setOptions(this, options);
  37. },
  38. onAdd: function (map) {
  39. this._crs = this.options.crs || map.options.crs;
  40. $L.TileLayer.prototype.onAdd.call(this, map);
  41. },
  42. getTileUrl: function (coords) { // (Point, Number) -> String
  43. var tileSize = this.options.tileSize;
  44. var nwPoint = coords.multiplyBy(tileSize);
  45. nwPoint.x+=1;
  46. nwPoint.y-=1;
  47. var sePoint = nwPoint.add(new $L.Point(tileSize, tileSize));
  48. var zoom = this._tileZoom;
  49. var nw = this._crs.project(this._map.unproject(nwPoint, zoom));
  50. var se = this._crs.project(this._map.unproject(sePoint, zoom));
  51. var tilewidth = se.x-nw.x;
  52. var ident = this.matrixIds[zoom].identifier;
  53. var tilematrix = this.wmtsParams.tilematrixset + ":" + ident;
  54. var X0 = this.matrixIds[zoom].topLeftCorner.lng;
  55. var Y0 = this.matrixIds[zoom].topLeftCorner.lat;
  56. var tilecol=Math.floor((nw.x-X0)/tilewidth);
  57. var tilerow=-Math.floor((nw.y-Y0)/tilewidth);
  58. var url = $L.Util.template(this._url, {s: this._getSubdomain(coords)});
  59. return url + $L.Util.getParamString(this.wmtsParams, url) + "&tilematrix=" + tilematrix + "&tilerow=" + tilerow +"&tilecol=" + tilecol;
  60. },
  61. setParams: function (params, noRedraw) {
  62. $L.extend(this.wmtsParams, params);
  63. if (!noRedraw) {
  64. this.redraw();
  65. }
  66. return this;
  67. },
  68. getDefaultMatrix : function () {
  69. /**
  70. * the matrix3857 represents the projection
  71. * for in the IGN WMTS for the google coordinates.
  72. */
  73. var matrixIds3857 = new Array(22);
  74. for (var i= 0; i<22; i++) {
  75. matrixIds3857[i]= {
  76. identifier : "" + i,
  77. topLeftCorner : new $L.LatLng(20037508.3428,-20037508.3428)
  78. };
  79. }
  80. return matrixIds3857;
  81. }
  82. });
  83. $L.tileLayer.wmts = function (url, options) {
  84. return new $L.TileLayer.WMTS(url, options);
  85. };
  86. /***
  87. * 插件移植结束
  88. */

其实就是把L改成了$L,这样就可以访问了!

这里把JSMap.vue贴一下

  1. <template>
  2. <div>
  3. <div id="map">
  4. </div>
  5. </div>
  6. </template>
  7. <script>
  8. import "leaflet/dist/leaflet.css"
  9. import $L from "leaflet";
  10. export default {
  11. mounted(){
  12. /***
  13. * 插件移植开始
  14. */
  15. $L.TileLayer.WMTS = $L.TileLayer.extend({
  16. defaultWmtsParams: {
  17. service: 'WMTS',
  18. request: 'GetTile',
  19. version: '1.0.0',
  20. layer: '',
  21. style: '',
  22. tilematrixset: '',
  23. format: 'image/jpeg'
  24. },
  25. initialize: function (url, options) { // (String, Object)
  26. this._url = url;
  27. var lOptions= {};
  28. var cOptions = Object.keys(options);
  29. cOptions.forEach(element=>{
  30. lOptions[element.toLowerCase()]=options[element];
  31. });
  32. var wmtsParams = $L.extend({}, this.defaultWmtsParams);
  33. var tileSize = lOptions.tileSize || this.options.tileSize;
  34. if (lOptions.detectRetina && $L.Browser.retina) {
  35. wmtsParams.width = wmtsParams.height = tileSize * 2;
  36. } else {
  37. wmtsParams.width = wmtsParams.height = tileSize;
  38. }
  39. for (var i in lOptions) {
  40. // all keys that are in defaultWmtsParams options go to WMTS params
  41. if (wmtsParams.hasOwnProperty(i) && i!="matrixIds") {
  42. wmtsParams[i] = lOptions[i];
  43. }
  44. }
  45. this.wmtsParams = wmtsParams;
  46. this.matrixIds = options.matrixIds||this.getDefaultMatrix();
  47. $L.setOptions(this, options);
  48. },
  49. onAdd: function (map) {
  50. this._crs = this.options.crs || map.options.crs;
  51. $L.TileLayer.prototype.onAdd.call(this, map);
  52. },
  53. getTileUrl: function (coords) { // (Point, Number) -> String
  54. var tileSize = this.options.tileSize;
  55. var nwPoint = coords.multiplyBy(tileSize);
  56. nwPoint.x+=1;
  57. nwPoint.y-=1;
  58. var sePoint = nwPoint.add(new $L.Point(tileSize, tileSize));
  59. var zoom = this._tileZoom;
  60. var nw = this._crs.project(this._map.unproject(nwPoint, zoom));
  61. var se = this._crs.project(this._map.unproject(sePoint, zoom));
  62. var tilewidth = se.x-nw.x;
  63. var ident = this.matrixIds[zoom].identifier;
  64. var tilematrix = this.wmtsParams.tilematrixset + ":" + ident;
  65. var X0 = this.matrixIds[zoom].topLeftCorner.lng;
  66. var Y0 = this.matrixIds[zoom].topLeftCorner.lat;
  67. var tilecol=Math.floor((nw.x-X0)/tilewidth);
  68. var tilerow=-Math.floor((nw.y-Y0)/tilewidth);
  69. var url = $L.Util.template(this._url, {s: this._getSubdomain(coords)});
  70. return url + $L.Util.getParamString(this.wmtsParams, url) + "&tilematrix=" + tilematrix + "&tilerow=" + tilerow +"&tilecol=" + tilecol;
  71. },
  72. setParams: function (params, noRedraw) {
  73. $L.extend(this.wmtsParams, params);
  74. if (!noRedraw) {
  75. this.redraw();
  76. }
  77. return this;
  78. },
  79. getDefaultMatrix : function () {
  80. /**
  81. * the matrix3857 represents the projection
  82. * for in the IGN WMTS for the google coordinates.
  83. */
  84. var matrixIds3857 = new Array(22);
  85. for (var i= 0; i<22; i++) {
  86. matrixIds3857[i]= {
  87. identifier : "" + i,
  88. topLeftCorner : new $L.LatLng(20037508.3428,-20037508.3428)
  89. };
  90. }
  91. return matrixIds3857;
  92. }
  93. });
  94. $L.tileLayer.wmts = function (url, options) {
  95. return new $L.TileLayer.WMTS(url, options);
  96. };
  97. /***
  98. * 插件移植结束
  99. */
  100. const ign = new $L.TileLayer.WMTS( "http://XXX.XXX.XXX.XXX:8080/geoserver/gwc/service/wmts" ,
  101. {
  102. layer: 'GG_9:gg_9',
  103. tilematrixset: "EPSG:900913",
  104. Format : 'image/png',
  105. TileMatrix: 'EPSG:900913:8'
  106. }
  107. );
  108. const map = $L.map('map', {
  109. minZoom: 4,
  110. maxZoom: 7
  111. }).setView([32, 118], 7);
  112. $L.control.scale({'position':'bottomleft','metric':true,'imperial':false}).addTo(map);
  113. map.addLayer(ign);
  114. map.invalidateSize(true);
  115. }
  116. }
  117. </script>
  118. <style>
  119. #map {
  120. width: 800px;
  121. height: 800px;
  122. }
  123. .chart{
  124. width: 600px;
  125. height: 300px;
  126. background-color: #fff;
  127. }
  128. </style>

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载