VTLayer.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import Layer from 'ol/layer/VectorTile';
  2. import Vector from 'ol/layer/Vector';
  3. import VectorSource from 'ol/source/Vector';
  4. import Source from 'ol/source/VectorTile';
  5. import Common from './Common';
  6. import MVT from 'ol/format/MVT';
  7. import {createXYZ} from 'ol/tilegrid'
  8. import KBaseObject from './KBaseObject'
  9. /**
  10. * @description KMap.VectorLayer 矢量图层
  11. */
  12. class VTLayer extends KBaseObject{
  13. /**
  14. * @param {string} name 图层名称
  15. * @param {string} zIndex 图层层级
  16. */
  17. constructor(url,options,zIndex,callback,mapInstance = null){
  18. super(mapInstance)
  19. const vm = this;
  20. let styleFunVT = options.styleFunVT;
  21. let styleFunV = options.styleFunV;
  22. let funThis = options.funThis;
  23. var source = new Source({
  24. format: new MVT(),
  25. url: url,
  26. tileGrid: createXYZ({
  27. tileSize: options.tileSize || 4096 // 瓦片大小
  28. }),
  29. tileLoadFunction:(tile, url)=>{
  30. tile.setLoader(function(extent, resolution, projection) {
  31. fetch(url).then(function(response) {
  32. response.arrayBuffer().then(function(data) {
  33. const format = tile.getFormat();
  34. const features = format.readFeatures(data, {
  35. extent: extent,
  36. featureProjection: projection
  37. });
  38. tile.setFeatures(features);
  39. callback(features);
  40. });
  41. });
  42. });
  43. }
  44. });
  45. let ShowLevel = Common.ShowLevel
  46. let minZoom = ShowLevel[0]
  47. let maxZoom = ShowLevel[1]
  48. if(options && options.minZoom){
  49. minZoom = options.minZoom
  50. }
  51. if(options && options.maxZoom){
  52. maxZoom = options.maxZoom
  53. }
  54. let layer = new Layer({
  55. source: source,
  56. zIndex: zIndex,
  57. minZoom: minZoom,
  58. maxZoom: maxZoom,
  59. style:(evt)=>{
  60. return styleFunVT.call(funThis,evt);
  61. }
  62. })
  63. this.layer = layer
  64. this.source = source,
  65. this.vLayer = this.initVectorLayer(styleFunV,funThis,minZoom,maxZoom)
  66. }
  67. setMaxZoom(maxZoom){
  68. this.layer.setMaxZoom(maxZoom);
  69. }
  70. setMinZoom(minZoom){
  71. this.layer.setMinZoom(minZoom);
  72. }
  73. initVectorLayer(styleFunV,funThis,minZoom,maxZoom){
  74. let layer = new Vector({
  75. source:new VectorSource({}),
  76. style:(evt)=>{
  77. return styleFunV.call(funThis,evt);
  78. },
  79. minZoom: minZoom,
  80. maxZoom: maxZoom,
  81. zIndex:101
  82. })
  83. return layer;
  84. }
  85. }
  86. export default VTLayer