MapHelper.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import proj4 from "proj4";
  2. import { Notification, MessageBox, Message, Loading } from "element-ui";
  3. const CTservice = axios.create({
  4. // 超时
  5. timeout: 60000,
  6. });
  7. // 响应拦截器
  8. CTservice.interceptors.response.use(
  9. (res) => {
  10. // 未设置状态码则默认成功状态
  11. const code = res.data.code || 200;
  12. // 二进制数据则直接返回
  13. if (
  14. res.request.responseType === "blob" ||
  15. res.request.responseType === "arraybuffer"
  16. ) {
  17. return res.data;
  18. }
  19. if (code !== 200) {
  20. Message({
  21. message: "模型服务获取失败,请检查服务",
  22. type: "warning",
  23. });
  24. return null;
  25. } else {
  26. return res;
  27. }
  28. },
  29. (error) => {
  30. console.log("err" + error);
  31. Message({
  32. message: "模型服务获取失败,请检查服务",
  33. type: "warning",
  34. });
  35. return null;
  36. }
  37. );
  38. export const mapQuery = async (url, queryObj) => {
  39. let response = await CTservice.post(url, queryObj);
  40. if (response && response.data.featureCount > 0) {
  41. return response.data;
  42. } else {
  43. return null;
  44. }
  45. };
  46. /**
  47. * 墨卡托转经纬度
  48. * @author
  49. * @param {*} x
  50. * @param {*} y
  51. * @returns [x,y]
  52. */
  53. export const mercator2lonLat = (x, y) => {
  54. var obj = proj4(proj4("EPSG:3857"), proj4("EPSG:4326"), [x, y]);
  55. return obj;
  56. };
  57. /**
  58. * 经纬度转墨卡托
  59. * @author
  60. * @param {*} x
  61. * @param {*} y
  62. * @returns [x,y]
  63. */
  64. export const lonLat2Mercator = (x, y) => {
  65. var obj = proj4(proj4("EPSG:4326"), proj4("EPSG:3857"), [x, y]);
  66. return obj;
  67. };
  68. /**
  69. * 笛卡尔坐标系转WGS84坐标系(经纬度)
  70. * @author
  71. * @param {object} point 点,笛卡尔坐标{x:x,y:y,z:z}
  72. * @returns {object} -lat: lat, lng: lng, alt: alt
  73. */
  74. export const cartesian3ToWGS84 = (point) => {
  75. var cartesian3 = new Cesium.Cartesian3(point.x, point.y, point.z);
  76. var cartographic = Cesium.Cartographic.fromCartesian(cartesian3);
  77. var lat = Cesium.Math.toDegrees(cartographic.latitude);
  78. var lng = Cesium.Math.toDegrees(cartographic.longitude);
  79. var alt = cartographic.height;
  80. return {
  81. lat: lat,
  82. lng: lng,
  83. alt: alt,
  84. };
  85. };
  86. /**
  87. * 对象数组扁平化
  88. * @param {array} arr 数组
  89. * @param {string} children
  90. * @returns
  91. */
  92. export const flatten = (arr, children = "children") => {
  93. return arr.reduce((result, item) => {
  94. return result.concat(
  95. item,
  96. Array.isArray(item[children]) ? flatten(item[children], children) : []
  97. );
  98. }, []);
  99. };
  100. /**
  101. * @author
  102. * @description 设置地下模式状态
  103. * @param {*} state
  104. */
  105. export const undergroundMode = (state) => {
  106. if (state) {
  107. viewer.scene.globe.globeAlpha = 0;
  108. } else {
  109. viewer.scene.globe.globeAlpha = 1;
  110. }
  111. // this.scene.undergroundMode = state; //设置开启地下场景
  112. };
  113. /**
  114. * @namespace 名称:getGroundPoint
  115. * @description 作用:根据经纬度(或者墨卡托)获取地面三维坐标
  116. * @param {*} lng 经度,
  117. * @param {*} lat 纬度,
  118. * @returns {Cartesian3} 返回笛卡尔坐标
  119. */
  120. export const getGroundPoint = async (lng, lat, completed) => {
  121. if (!lng || !lat) return null;
  122. try {
  123. debugger;
  124. let positions = [Cesium.Cartographic.fromDegrees(lng, lat)];
  125. //如果有dem高程,则取dem高程,如果没有,则地图面高度为0
  126. if (viewer.terrainProvider) {
  127. var promise = Cesium.sampleTerrainMostDetailed(
  128. viewer.terrainProvider,
  129. positions
  130. );
  131. const resultP = await Promise.resolve(promise);
  132. if (resultP && resultP.length > 0) {
  133. let myP = new Cesium.Cartesian3.fromDegrees(
  134. lng,
  135. lat,
  136. resultP[0].height
  137. );
  138. return myP;
  139. }
  140. }
  141. } catch (error) {
  142. return null;
  143. }
  144. let myP = Cesium.Cartesian3.fromDegrees(lng, lat, 0);
  145. return myP;
  146. };