当前位置:   article > 正文

uniapp 地图分幅网格生成 小程序基于map组件

uniapp 地图分幅网格生成 小程序基于map组件
  1. // 获取小数部分
  2. const fractional = function(x) {
  3. x = Math.abs(x);
  4. return x - Math.floor(x);
  5. }
  6. const formatInt = function(x, len) {
  7. let result = '' + x;
  8. len = len - result.length;
  9. while (len > 0) {
  10. result = '0' + result;
  11. len--;
  12. }
  13. return result;
  14. }
  15. /**
  16. * 创建标准分幅网格
  17. * @param west,south,east,north 传入要创建的标准分幅网格的经纬度范围
  18. * @param scalem 表示比例尺的分母(例如 10000 表示 1:1万)
  19. * @returns 返回一个 geojson 对象
  20. */
  21. export function makeStandardMapGrids(west, south, east, north, scalem) {
  22. let lngDiff = 0;
  23. let latDiff = 0;
  24. let scaleCode = '';
  25. switch (scalem) {
  26. case 1000000:
  27. lngDiff = 6;
  28. latDiff = 4;
  29. break;
  30. case 500000:
  31. lngDiff = 3;
  32. latDiff = 2;
  33. scaleCode = 'B';
  34. break;
  35. case 250000:
  36. lngDiff = 1.5;
  37. latDiff = 1;
  38. scaleCode = 'C';
  39. break;
  40. case 100000:
  41. lngDiff = 0.5;
  42. latDiff = 1 / 3;
  43. scaleCode = 'D';
  44. break;
  45. case 50000:
  46. lngDiff = 0.25;
  47. latDiff = 1 / 6;
  48. scaleCode = 'E';
  49. break;
  50. case 25000:
  51. lngDiff = 0.125;
  52. latDiff = 1 / 12;
  53. scaleCode = 'F';
  54. break;
  55. case 10000:
  56. lngDiff = 0.0625;
  57. latDiff = 1 / 24;
  58. scaleCode = 'G';
  59. break;
  60. case 5000:
  61. lngDiff = 0.03125;
  62. latDiff = 1 / 48;
  63. scaleCode = 'H';
  64. case 2000:
  65. lngDiff = 37.5 / 3600.0;
  66. latDiff = 25.0 / 3600.0;
  67. scaleCode = 'I';
  68. break;
  69. case 1000:
  70. lngDiff = 18.75 / 3600.0;
  71. latDiff = 12.5 / 3600.0;
  72. scaleCode = 'J';
  73. break;
  74. case 500:
  75. lngDiff = 9.375 / 3600.0;
  76. latDiff = 6.25 / 3600.0;
  77. scaleCode = 'K';
  78. break;
  79. default:
  80. return null;
  81. }
  82. const GridX0 = -180;
  83. const GridX1 = 180;
  84. const GridY0 = -88;
  85. const GridY1 = 88;
  86. let x0 = Math.max(GridX0, west);
  87. let y0 = Math.max(GridY0, south);
  88. let x1 = Math.min(GridX1, east);
  89. let y1 = Math.min(GridY1, north);
  90. if (((x1 - x0) < lngDiff) || ((y1 - y0) < latDiff)) {
  91. return null;
  92. }
  93. let features = []; // 存储生成的面要素
  94. let coordinates = [] // 存储生成的面要素坐标对
  95. // 计算标准分幅网格行列范围
  96. const col0 = parseInt((x0 - GridX0) / lngDiff);
  97. const col1 = parseInt((x1 - GridX0) / lngDiff);
  98. const row0 = parseInt((y0 - GridY0) / latDiff);
  99. const row1 = parseInt((y1 - GridY0) / latDiff);
  100. const millionRowCode = 'ABCDEFGHIJKLMNOPQRSTUV';
  101. for (let row = row0; row <= row1; row++) {
  102. let gy0 = GridY0 + row * latDiff;
  103. let gy1 = gy0 + latDiff;
  104. let gcy = (gy0 + gy1) * 0.5; // 分幅中心点 y 坐标
  105. let millionRow = parseInt((gy0 - 0) / 4); // 1:100分幅行号
  106. let Hemisphere = ''; // 北半球标志
  107. if (millionRow < 0) {
  108. millionRow = -1 - millionRow;
  109. Hemisphere = 'S'; // 南半球标志
  110. }
  111. for (let col = col0; col <= col1; col++) {
  112. let gx0 = GridX0 + col * lngDiff;
  113. let gx1 = gx0 + lngDiff;
  114. let gcx = (gx0 + gx1) * 0.5; // 分幅中心点 x 坐标
  115. let millionCol = parseInt((gcx - GridX0) / 6) + 1; // 1:100分幅列号(从1开始)
  116. coordinates = [
  117. [
  118. [gx0, gy0],
  119. [gx1, gy0],
  120. [gx1, gy1],
  121. [gx0, gy1],
  122. [gx0, gy0]
  123. ]
  124. ];
  125. millionCol = (millionCol < 10) ? ('0' + millionCol) : millionCol;
  126. let gridID = Hemisphere + millionRowCode[millionRow] + millionCol;
  127. if (scaleCode != '') {
  128. // 计算当前分幅在 1:100万 分幅内的行列号(注意,这里行列号从左向右,从北向南,从1开始编号)
  129. let colID = parseInt((fractional((gcx - GridX0) / 6) * 6) / lngDiff) + 1;
  130. let rowID = parseInt((fractional((GridY1 - gcy) / 4) * 4) / latDiff) + 1;
  131. gridID += scaleCode + formatInt(rowID, 3) + formatInt(colID, 3);
  132. }
  133. // 生成矢量要素(几何信息+属性信息)
  134. let feature = {
  135. type: "Feature",
  136. geometry: {
  137. type: "Polygon",
  138. coordinates: coordinates
  139. },
  140. properties: {
  141. ID: gridID,
  142. extend: '西:' + gx0 + ' 东:' + gx1 + ' 南:' + gy0 + ' 北:' + gy1,
  143. lat: gcy.toFixed(6),
  144. lng: gcx.toFixed(6)
  145. },
  146. location: {
  147. latitude: Number(gcy.toFixed(6)),
  148. longitude: Number(gcx.toFixed(6)),
  149. }
  150. };
  151. features.push(feature);
  152. }
  153. }
  154. return {
  155. type: "FeatureCollection",
  156. features: features
  157. };
  158. }

 我国把 1:1 万、1:2.5 万、1:5 万、1:10 万、1:25 万、1:50 万、1:100 万 7 种比例尺作为国家基本地图的比例尺系列。根据国家标准GB/13989-92 《国家基本比例尺地形图分幅和编号》规定,我国基本比例尺地形图均1:100 万地形图为基础,按规定的经差和纬差划分图幅。下表为地形图的经纬差、行列数及图幅数。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/397373
推荐阅读
相关标签
  

闽ICP备14008679号