CrMap.js 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  1. /* 引入Cesium */
  2. import * as Cesium from 'cesium';
  3. /* 引入jquery 这个必须使用 否则导致后期使用$ $.fn. 的内容全部失效*/
  4. import $ from 'jquery';
  5. /* 引入 */
  6. import ArcgisImageryProvider from './ArcgisImageryProvider.js';
  7. /* 引入自定义图层 */
  8. import CrImageServerLayer from './CrImageServerLayer.js';
  9. // /* 扩展更改属性 */
  10. // Cesium.TileCoordinatesImageryProvider.prototype.requestImage = function(
  11. // x,
  12. // y,
  13. // level,
  14. // request
  15. // ) {
  16. // const canvas = document.createElement("canvas");
  17. // canvas.width = 256;
  18. // canvas.height = 256;
  19. // const context = canvas.getContext("2d");
  20. // const cssColor = this._color.toCssColorString();
  21. // context.strokeStyle = cssColor;
  22. // context.lineWidth = 2;
  23. // context.strokeRect(1, 1, 255, 255);
  24. // context.font = "bold 25px Arial";
  25. // context.textAlign = "center";
  26. // context.fillStyle = cssColor;
  27. // context.fillText(`L: ${level}`, 124, 86);
  28. // context.fillText(`X: ${x}`, 124, 136);
  29. // context.fillText(`Y: ${y}`, 124, 186);
  30. // /* 填充图片试试 */
  31. // let baseUrl =
  32. // 'http://218.59.194.74:6080/arcgis/rest/services/LYLSQ_GHT_102100_202112/MapServer/Tile/';
  33. // let imageObj = new Image();
  34. // imageObj.crossOrigin = "anonymous"; // 添加这行代码
  35. // imageObj.width = 256;
  36. // imageObj.height = 256;
  37. // imageObj.style.opacity = 0.3;
  38. // /* 做个转换 */
  39. // let rectangle = this._tilingScheme.tileXYToNativeRectangle(x, y, level);
  40. // // if (this.onRequestRectangle) this.onRequestRectangle(x, y, level, rectangle);
  41. // // return Promise.resolve(canvas);
  42. // return new Promise(function(resolve, reject) {
  43. // imageObj.onload = function() {
  44. // console.log('===>>>', '填充图片');
  45. // context.fillStyle = context.createPattern(imageObj, 'no-repeat');
  46. // context.fillRect(0, 0, 256, 256);
  47. // resolve(canvas);
  48. // // let entity = new Cesium.Entity({
  49. // // rectangle: {
  50. // // coordinates: rectangle,
  51. // // material: Cesium.Color.RED,
  52. // // }
  53. // // });
  54. // // resolve(entity);
  55. // }
  56. // imageObj.src = baseUrl + level + '/' + y + '/' + x;
  57. // })
  58. // };
  59. class CrMap {
  60. /**
  61. * 默认初始化
  62. * @param {JSON} options 配置项
  63. * @param {String} options.selector 加载地图的Dom空间Id
  64. * @param {String} options.sourcePath 资源目录
  65. */
  66. constructor(options) {
  67. let _self = this;
  68. /* 设置token 这很重要 否则将导致地图无法加载 */
  69. Cesium.Ion.defaultAccessToken =
  70. 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIxNzM5YjQ3MC03YmMxLTRmMjAtOTk4Yi0yNDMyMDZlOTQzYTYiLCJpZCI6NTU5MjAsImlhdCI6MTYyNDI0NTM5NX0.diydVWFzw5C5rQlHaFYkdDJoSorcdex81KpWcntyICo';
  71. Cesium.buildModuleUrl.setBaseUrl(options.sourcePath);
  72. console.log("三维资源目录", Cesium.buildModuleUrl.getCesiumBaseUrl());
  73. /* 设置地图容器 */
  74. this._selector = options.selector;
  75. /* 初始化地图控件 */
  76. this._viewer = this._initMap();
  77. /* 注册事件 */
  78. // this._registerEvent();
  79. /* 初始化资源 */
  80. this._initResource();
  81. /* 初始化变量 地图基础图层集合*/
  82. this._imageryLayers = this._viewer.imageryLayers;
  83. /* 初始化变量 地图原语集合 */
  84. this._primitives = this._viewer.scene.primitives;
  85. /* 初始化变量 地图绘制实体集合 */
  86. this._entities = this._viewer.entities;
  87. /* 初始化变量 数据源集合 */
  88. this._dataSources = this._viewer.dataSources; //数据源集合
  89. /* 注册事件 */
  90. this._viewer.scene.postRender.addEventListener(sss => {
  91. });
  92. this._viewer.camera.changed.addEventListener(function() {
  93. // console.log('===>>>', '相机变化');
  94. })
  95. this._viewer.camera.moveEnd.addEventListener(function(event) {
  96. // console.log('===>>>', '相机移动');
  97. // console.log('===>>>', _self._viewer.scene.globe);
  98. // _self._drawTileGrid();
  99. });
  100. }
  101. _drawTileGrid() {
  102. let _self = this;
  103. let tilesToRender = _self._viewer.scene.globe._surface._tilesToRender;
  104. let renderRectangles = {};
  105. let mercatorTilingScheme = new Cesium.WebMercatorTilingScheme();
  106. for (let i = 0; i < tilesToRender.length; i++) {
  107. let render = tilesToRender[i];
  108. let imageryLevel = render.level;
  109. /* 获取矩形 */
  110. let rectangle = render._rectangle;
  111. /* 判断北西角点的墨卡托投影瓦片信息 */
  112. let northwestTileCoordinates = mercatorTilingScheme.positionToTileXY(
  113. Cesium.Rectangle.northwest(rectangle),
  114. imageryLevel
  115. );
  116. if (northwestTileCoordinates !== undefined) {
  117. let _webRectangle = mercatorTilingScheme.tileXYToRectangle(northwestTileCoordinates.x,
  118. northwestTileCoordinates.y,
  119. imageryLevel);
  120. let jsonIndex = '[' + northwestTileCoordinates.x + ',' + northwestTileCoordinates.y + ',' +
  121. imageryLevel + ']';
  122. if (renderRectangles[jsonIndex] === undefined) {
  123. renderRectangles[jsonIndex] = {
  124. x: northwestTileCoordinates.x,
  125. y: northwestTileCoordinates.y,
  126. level: imageryLevel,
  127. rectangle: _webRectangle,
  128. lineColor: Cesium.Color.YELLOW,
  129. }
  130. }
  131. }
  132. /* 判断南东角点的墨卡托投影瓦片信息 */
  133. let southeastTileCoordinates = mercatorTilingScheme.positionToTileXY(
  134. Cesium.Rectangle.southeast(rectangle),
  135. imageryLevel
  136. );
  137. if (southeastTileCoordinates !== undefined) {
  138. let _webRectangle = mercatorTilingScheme.tileXYToRectangle(southeastTileCoordinates.x,
  139. southeastTileCoordinates.y,
  140. imageryLevel);
  141. let jsonIndex = '[' + southeastTileCoordinates.x + ',' + southeastTileCoordinates.y + ',' +
  142. imageryLevel +
  143. ']';
  144. if (renderRectangles[jsonIndex] === undefined) {
  145. renderRectangles[jsonIndex] = {
  146. x: southeastTileCoordinates.x,
  147. y: southeastTileCoordinates.y,
  148. level: imageryLevel,
  149. rectangle: _webRectangle,
  150. lineColor: Cesium.Color.YELLOW,
  151. }
  152. }
  153. }
  154. }
  155. console.log('===>>>', renderRectangles);
  156. if (JSON.stringify(renderRectangles) !== '{}') {
  157. _self._appendRectangleFillImage(renderRectangles);
  158. }
  159. }
  160. _appendRectangle(tilesJson) {
  161. let _self = this;
  162. let entityName = 'tilesRectangle';
  163. this._removeEntityByName(entityName);
  164. for (let tile in tilesJson) {
  165. let tileJson = tilesJson[tile];
  166. let tileRow = tileJson.x;
  167. let tileColumn = tileJson.y;
  168. let tileLevel = tileJson.level;
  169. let entityId = tileRow + "_" + tileColumn + "_" + tileLevel;
  170. /* 查询实体是否存在 */
  171. if (_self._entities.getById(entityId) === undefined) {
  172. let bjPositions = _self._calculateRectangleOutlineCoordinates(tileJson
  173. .rectangle);
  174. let tileEntity = new Cesium.Entity({
  175. name: entityName,
  176. id: entityId,
  177. rectangle: {
  178. coordinates: tileJson.rectangle,
  179. material: Cesium.Color.RED.withAlpha(0.0),
  180. },
  181. polyline: {
  182. positions: bjPositions,
  183. material: tileJson.lineColor,
  184. width: 5,
  185. clampToGround: true, //开启贴地 如果有模型则贴模型
  186. }
  187. });
  188. // console.log('===创建的矩形>>>', entityId);
  189. _self._entities.add(tileEntity);
  190. }
  191. }
  192. }
  193. _appendRectangleFillImage(tilesJson) {
  194. let _self = this;
  195. let entityName = 'tilesRectangle';
  196. this._removeEntityByName(entityName);
  197. let baseUrl =
  198. 'http://218.59.194.74:6080/arcgis/rest/services/LYLSQ_GHT_102100_202112/MapServer/Tile/';
  199. for (let tile in tilesJson) {
  200. let tileJson = tilesJson[tile];
  201. let tileRow = tileJson.x;
  202. let tileColumn = tileJson.y;
  203. let tileLevel = tileJson.level;
  204. let entityId = tileRow + "_" + tileColumn + "_" + tileLevel;
  205. /* 查询实体是否存在 */
  206. if (_self._entities.getById(entityId) === undefined) {
  207. let imageObj = new Image();
  208. imageObj.crossOrigin = "anonymous"; // 添加这行代码
  209. imageObj.width = 256;
  210. imageObj.height = 256;
  211. imageObj.style.opacity = 0.3;;
  212. imageObj.onload = function() {
  213. let material = new Cesium.ImageMaterialProperty({
  214. image: imageObj,
  215. transparent: true,
  216. });
  217. let bjPositions = _self._calculateRectangleOutlineCoordinates(tileJson
  218. .rectangle);
  219. let tileEntity = new Cesium.Entity({
  220. name: entityName,
  221. id: entityId,
  222. rectangle: {
  223. coordinates: tileJson.rectangle,
  224. material: material, //Cesium.Color.RED.withAlpha(0.9),
  225. },
  226. polyline: {
  227. positions: bjPositions,
  228. material: Cesium.Color.BLUE,
  229. // width: 2,
  230. clampToGround: true, //开启贴地 如果有模型则贴模型
  231. }
  232. });
  233. console.log('===创建的矩形>>>', entityId);
  234. _self._entities.add(tileEntity);
  235. }
  236. imageObj.src = baseUrl + tileLevel + '/' + tileColumn + '/' + tileRow;
  237. }
  238. }
  239. }
  240. /**
  241. * 计算矩形的外围坐标串
  242. * @param {Cesium.Rectangle} rectangle 矩形
  243. * @return {Array<Cesium.Cartesian3>} 坐标串集合
  244. */
  245. _calculateRectangleOutlineCoordinates(rectangle) {
  246. /* 计算东南角 */
  247. let south_east = Cesium.Rectangle.southeast(rectangle);
  248. let se = Cesium.Cartographic.toCartesian(south_east);
  249. /* 计算西南角 */
  250. let south_west = Cesium.Rectangle.southwest(rectangle);
  251. let sw = Cesium.Cartographic.toCartesian(south_west);
  252. /* 计算东北角 */
  253. let north_east = Cesium.Rectangle.northeast(rectangle);
  254. let ne = Cesium.Cartographic.toCartesian(north_east);
  255. /* 计算西北角 */
  256. let north_west = Cesium.Rectangle.northwest(rectangle);
  257. let nw = Cesium.Cartographic.toCartesian(north_west);
  258. return [sw, se, ne, nw, sw];
  259. }
  260. /**
  261. * 初始化地图
  262. */
  263. _initMap() {
  264. let viewer = new Cesium.Viewer(this._selector, {
  265. animation: false, //是否显示动画控件
  266. baseLayerPicker: false, //地图切换控件(底图以及地形图)是否显示,默认显示true
  267. timeline: false, //是否显示时间线控件,默认true
  268. navigationHelpButton: false, //是否显示帮助信息控件
  269. sceneModePicker: false, //是否显示3D/2D选择器
  270. infoBox: false, //点击要素之后显示的信息,默认true
  271. clampToGround: true, //开启贴地
  272. homeButton: false, //主页按钮,默认true
  273. geocoder: false, //地名查找,默认true
  274. scene3DOnly: true, //如果设置为true,则所有几何图形以3D模式绘制以节约GPU资源
  275. fullscreenButton: false, //全屏按钮,默认显示true
  276. shadows: false, //阴影
  277. imageryProvider: new Cesium.SingleTileImageryProvider({
  278. url: Cesium.buildModuleUrl('Assets/Images/earth_3.jpg'),
  279. }),
  280. selectionIndicator: false, //选中元素显示,默认true选中元素显示,默认true
  281. });
  282. viewer._cesiumWidget._creditContainer.style.display = "none"; //隐藏版本信息
  283. /* 深度检测开启/关闭 此项设置为true可以查看哪些内容掉到地下了,默认情况下为false 否则会导致绘制的点只能看见一半 */
  284. viewer.scene.globe.depthTestAgainstTerrain = true; //地形不透明 地形检测
  285. viewer.imageryLayers.get(0).show = true; //删除默认加载的影像,显示蓝色地球
  286. viewer.scene.skyBox.show = false; //是否显示星空
  287. viewer.scene.sun.show = true; //是否显示太阳
  288. viewer.scene.moon.show = false; //是否显示有月亮
  289. viewer.scene.skyAtmosphere.show = true; //是否隐藏大气圈
  290. viewer.scene.globe.show = true; //是否显示地球
  291. viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType
  292. .LEFT_DOUBLE_CLICK); //取消鼠标左键双击
  293. viewer.scene.debugShowFramesPerSecond = true; //是否显示帧FPS
  294. viewer.scene.requestRenderMode = false; //启动主动渲染 这样地图加载完成后停止渲染 可节省CPU的开支
  295. /* 开启精细渲染 不要太大 否则会导致效率下降 对WEB系统不太适应*/
  296. // viewer._cesiumWidget._supportsImageRenderingPixelated = Cesium.FeatureDetection
  297. // .supportsImageRenderingPixelated();
  298. // viewer._cesiumWidget._forceResize = true;
  299. // if (Cesium.FeatureDetection.supportsImageRenderingPixelated()) {
  300. // var vtxf_dpr = window.devicePixelRatio;
  301. // // 适度降低分辨率
  302. // while (vtxf_dpr >= 2.0) {
  303. // vtxf_dpr /= 2.0;
  304. // }
  305. // // alert(vtxf_dpr);
  306. // viewer.resolutionScale = 2.6;
  307. // }
  308. return viewer;
  309. }
  310. /**
  311. * 初始化资源
  312. */
  313. _initResource() {
  314. /* 草图工具绘制的点图片 */
  315. this._sketchPointImage =
  316. 'data:image/png;base64,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';
  317. }
  318. /**
  319. * 注册事件
  320. */
  321. _registerEvent() {
  322. let _self = this;
  323. let handler = new Cesium.ScreenSpaceEventHandler(this._viewer.scene.canvas);
  324. /* 挂接点击事件监听 */
  325. handler.setInputAction(function(event) {
  326. let res = _self._getLocation(event.position);
  327. console.log('点击位置', JSON.stringify(res));
  328. }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  329. }
  330. /**
  331. * 弧度转度
  332. * @param {Number} arc 弧度
  333. * @return {Number} 角度
  334. */
  335. _arcToDegree(arc) {
  336. return arc / Math.PI * 180;
  337. }
  338. /**
  339. * 普通颜色值转换为Cesium颜色
  340. * @param {int} red 红色[0~255]
  341. * @param {int} green 绿色[0~255]
  342. * @param {int} blue 蓝色[0~255]
  343. * @param {int} alpha 透明度[0~1]
  344. */
  345. _toColor(red, green, blue, alpha) {
  346. return new Cesium.Color(red / 255.0, green / 255.0, blue / 255.0, alpha);
  347. }
  348. /**
  349. * 根据Entity的名称批量删除Entity
  350. * @param {string} entityName 实体名称
  351. */
  352. _removeEntityByName(entityName) {
  353. /* 获取实体集合 */
  354. var entities = this._entities;
  355. /* 如果不存在实体集合或集合中没有数据 则返回 */
  356. if (!entities || !entities.values) return;
  357. var delEntitys = [];
  358. /* 循环获取当前集合中的所有实体 */
  359. for (var i = 0; i < entities.values.length; i++) {
  360. if (entities.values[i].name == entityName) {
  361. delEntitys.push(entities.values[i]);
  362. }
  363. }
  364. /* 删除符合条件的所有实体 */
  365. for (var i = 0; i < delEntitys.length; i++) {
  366. entities.remove(delEntitys[i]);
  367. }
  368. /* 更新场景 */
  369. this.updateScene();
  370. }
  371. /**
  372. * 获取屏幕点的经纬度及高度 如果点击位置有模型则获取模型高度 否则获取地形高度
  373. * @param {JSON} screenPoint
  374. * @param {Number} screenPoint.x 屏幕坐标x
  375. * @param {Number} screenPoint.y 屏幕坐标y
  376. * @return {JSON} {lng:lat:height:}
  377. */
  378. _getLocation(screenPoint) {
  379. let res = {
  380. lng: undefined,
  381. lat: undefined,
  382. height: undefined
  383. }
  384. /* 从相机位置到 windowPosition 处的像素创建射线在世界坐标系中 */
  385. let ray = this._viewer.scene.camera.getPickRay(screenPoint);
  386. /* 找到射线与渲染的地球表面之间的交点 */
  387. let position = this._viewer.scene.globe.pick(ray, this._viewer.scene);
  388. /* 获取地理位置的制图表达 */
  389. let cartographic = Cesium.Ellipsoid.WGS84.cartesianToCartographic(position);
  390. /* 查询屏幕位置的要素 */
  391. let feature = this._viewer.scene.pick(screenPoint);
  392. if (!feature) {
  393. res.lng = this._arcToDegree(cartographic.longitude);
  394. res.lat = this._arcToDegree(cartographic.latitude);
  395. res.height = cartographic.height;
  396. } else {
  397. let cartesian = this._viewer.scene.pickPosition(screenPoint);
  398. if (Cesium.defined(cartesian)) {
  399. let cartographic = Cesium.Cartographic.fromCartesian(cartesian);
  400. res.lng = this._arcToDegree(cartographic.longitude);
  401. res.lat = this._arcToDegree(cartographic.latitude);
  402. res.height = cartographic.height;
  403. }
  404. }
  405. /* 返回结果 */
  406. return res;
  407. }
  408. /**
  409. * 根据地形或实景或模型检测当前屏幕位置的世界坐标系位置
  410. * @param {JSON} screenPosition 屏幕坐标
  411. * @param {Number} screenPosition.x 屏幕坐标x
  412. * @param {Number} screenPosition.y 屏幕坐标y
  413. */
  414. _getScreenClickPosition(screenPosition) {
  415. let resCartesian = undefined;
  416. /* 检测该位置是否存在实景或模型 */
  417. let feature = this._viewer.scene.pick(screenPosition);
  418. if (Cesium.defined(feature)) {
  419. /* 说明检测到了实景或模型对象 */
  420. let tempCartesian = this._viewer.scene.pickPosition(screenPosition);
  421. if (Cesium.defined(tempCartesian)) resCartesian = tempCartesian.clone();
  422. } else {
  423. /* 未检测到模型或实景 返回地形位置 */
  424. let ray = this._viewer.scene.camera.getPickRay(screenPosition);
  425. let tempCartesian = this._viewer.scene.globe.pick(ray, this._viewer.scene);
  426. if (Cesium.defined(tempCartesian)) resCartesian = tempCartesian.clone();
  427. }
  428. return resCartesian;
  429. }
  430. /**
  431. * 搜索指定Id的上下关联图层索引
  432. * @param {string} layerId 图层Id
  433. */
  434. _searchCorrelationLayerIndex(layerId) {
  435. if (this._layerConfigs == undefined) {
  436. this._layerConfigs = [];
  437. }
  438. var index = -1;
  439. for (var i in this._layerConfigs) {
  440. if (this._layerConfigs[i].layId == layerId) {
  441. index = parseInt(i);
  442. break;
  443. }
  444. }
  445. if (index == -1) return undefined;
  446. var minIndex = -1;
  447. var maxIndex = -1;
  448. /* 找该图层底部的图层 */
  449. for (var i = index - 1; i >= 0; i--) {
  450. minIndex = this._imageryLayers.indexOf(window[this._layerConfigs[i].layId]);
  451. if (minIndex != -1) {
  452. break;
  453. }
  454. }
  455. /* 找该图层顶部的图层 */
  456. for (var i = index + 1; i < this._layerConfigs.length; i++) {
  457. maxIndex = this._imageryLayers.indexOf(window[this._layerConfigs[i].layId]);
  458. if (maxIndex != -1) {
  459. break;
  460. }
  461. }
  462. /* 返回寻找结果 */
  463. return {
  464. minIndex: minIndex,
  465. maxIndex: maxIndex
  466. };
  467. }
  468. /**
  469. * 添加provider到地图中
  470. * @param {Object} provider 图层构建器
  471. * @param {Object} layerId 图层Id
  472. */
  473. _addImageryProvider(provider, layerId) {
  474. let id = layerId == undefined ? this._guid() : layerId;
  475. let resSearch = this._searchCorrelationLayerIndex(id);
  476. let resLayer = undefined;
  477. if (resSearch != undefined && resSearch.maxIndex != -1) {
  478. /* 找到该图层顶部的关联图层 则直接插入到该图层到底部 */
  479. resLayer = this._imageryLayers.addImageryProvider(provider, resSearch.maxIndex);
  480. } else if (resSearch != undefined && resSearch.minIndex != -1 && resSearch.minIndex != this
  481. .imageryLayers.length - 1) {
  482. /* 找到该图层底部关联的图层 且该图层不是最顶部的图层 则将该图层加载到这个图层之上 */
  483. resLayer = this._imageryLayers.addImageryProvider(provider, resSearch.minIndex + 1);
  484. } else {
  485. resLayer = this._imageryLayers.addImageryProvider(provider);
  486. }
  487. /* 加入到整体图层中 以便可以删除对应的图层 */
  488. window[id] = resLayer;
  489. /* 返回该图层 */
  490. return resLayer;
  491. }
  492. /**
  493. * 创建GUID
  494. */
  495. _guid() {
  496. function S4() {
  497. return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  498. }
  499. return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
  500. }
  501. /**
  502. * 三维模型旋转平移
  503. * @param {Object} tileset 三维模型数据
  504. * @param {JSON} params 参数配置项
  505. * @param {JSON} params.tx x方向平移
  506. * @param {JSON} params.ty y方向平移
  507. * @param {JSON} params.tz z方向平移
  508. * @param {JSON} params.rx x方向旋转
  509. * @param {JSON} params.ry y方向旋转
  510. * @param {JSON} params.rz z方向旋转
  511. */
  512. _update3dtilesMaxtrix(tileset, params) {
  513. //旋转
  514. var mx = Cesium.Matrix3.fromRotationX(Cesium.Math.toRadians(params.rx));
  515. var my = Cesium.Matrix3.fromRotationY(Cesium.Math.toRadians(params.ry));
  516. var mz = Cesium.Matrix3.fromRotationZ(Cesium.Math.toRadians(params.rz));
  517. var rotationX = Cesium.Matrix4.fromRotationTranslation(mx);
  518. var rotationY = Cesium.Matrix4.fromRotationTranslation(my);
  519. var rotationZ = Cesium.Matrix4.fromRotationTranslation(mz);
  520. //平移
  521. var position = Cesium.Cartesian3.fromDegrees(params.tx, params.ty, params.tz);
  522. var m = Cesium.Transforms.eastNorthUpToFixedFrame(position);
  523. //旋转、平移矩阵相乘
  524. Cesium.Matrix4.multiply(m, rotationX, m);
  525. Cesium.Matrix4.multiply(m, rotationY, m);
  526. Cesium.Matrix4.multiply(m, rotationZ, m);
  527. //赋值给tileset
  528. tileset._root.transform = m;
  529. }
  530. }
  531. /**
  532. * 工具扩展
  533. */
  534. Object.assign(CrMap.prototype, {
  535. /**
  536. * 异步飞行
  537. * @param {JSON} options 配置项
  538. * @param {Object} options.target 飞行目的地目标
  539. * @param {Int} options.duration 飞行持续时间 秒
  540. * @param {Float} options.radians 旋转角度 度
  541. * @param {Boolean} options.isRemove 执行完成后是否删除目标 true/false
  542. */
  543. asyncFlyTo: function(options) {
  544. var _self = this;
  545. return new Promise((resolve, reject) => {
  546. /* 飞行 */
  547. var flyPromise = _self._viewer.flyTo(options.target, {
  548. duration: options.duration, //飞行持续时间
  549. offset: new Cesium.HeadingPitchRange(0.0, Cesium.Math.toRadians(options
  550. .radians)),
  551. });
  552. /* 飞行完成 */
  553. flyPromise.then(function(isFly) {
  554. if (isFly && options.isRemove && options.target != undefined) {
  555. _self._viewer.entities.remove(options.target);
  556. }
  557. resolve(true);
  558. })
  559. });
  560. },
  561. /**
  562. * 飞向矩形区域
  563. * @param {JSON} options 配置项
  564. * @param {Float} options.strLng 矩形起始点经度
  565. * @param {Float} options.strLat 矩形起始点纬度
  566. * @param {Float} options.endLng 矩形终止点经度
  567. * @param {Float} options.endLat 矩形终止点纬度
  568. */
  569. flyToRectangle: function(options) {
  570. var _self = this;
  571. this._entities.removeById('flyRectangle');
  572. var extent = Cesium.Rectangle.fromDegrees(options.strLng, options.strLat, options.endLng,
  573. options.endLat);
  574. /* 创建飞行矩形 */
  575. var flyRectangle = this._entities.add({
  576. id: 'flyRectangle',
  577. name: 'flyRectangle',
  578. rectangle: {
  579. coordinates: extent,
  580. material: Cesium.Color.GREEN.withAlpha(0.0),
  581. height: 200.0,
  582. outline: false
  583. }
  584. });
  585. this.asyncFlyTo({
  586. target: flyRectangle, //定位对象
  587. isRemove: false, //定位完成后是否删除
  588. duration: 3,
  589. radians: -90,
  590. }).then(function(isFly) {
  591. if (isFly) {
  592. _self.asyncFlyTo({
  593. target: flyRectangle, //定位对象
  594. isRemove: true, //定位完成后是否删除
  595. duration: 1,
  596. radians: -40,
  597. }).then(function(isFly) {
  598. if (isFly && options.success != undefined) {
  599. options.success();
  600. }
  601. })
  602. }
  603. }).catch(function(err) {
  604. alert(err);
  605. })
  606. },
  607. /**
  608. * 设置显示范围
  609. * @param {JSON} options 配置项
  610. * @param {Float} options.lng 经度
  611. * @param {Float} options.lat 纬度
  612. * @param {Float} options.alt 高度
  613. * @param {Float} options.heading 指北角度 度
  614. * @param {Float} options.pitch 垂直视角 度
  615. * @param {Float} options.roll 翻滚视角 度
  616. */
  617. setMapRange: function(options) {
  618. this._viewer.camera.setView({
  619. destination: Cesium.Cartesian3.fromDegrees(options.lng, options.lat, options.alt),
  620. orientation: {
  621. //此视角为观察者/相机
  622. heading: options.heading,
  623. pitch: Cesium.Math.toRadians(options.pitch),
  624. roll: options.roll
  625. }
  626. });
  627. },
  628. /**
  629. * 刷新场景 刷新一帧
  630. */
  631. updateScene: function() {
  632. this._viewer.scene.requestRender();
  633. },
  634. /**
  635. * 调用相机方法飞到指定位置
  636. * @param {Object} options 配置项
  637. * @param {Number} options.lng 相机位置经度 度格式
  638. * @param {Number} options.lat 相机位置纬度 度格式
  639. * @param {Number} options.alt 相机位置高度 度格式
  640. * @param {Number} options.heading 方向角度 度格式
  641. * @param {Number} options.picth 俯仰角度 度格式
  642. * @param {Number} options.roll 滚动角度 度格式
  643. */
  644. cameraFlyToo: function(options) {
  645. this._viewer.camera.flyTo({
  646. destination: Cesium.Cartesian3.fromDegrees(options.lng, options.lat, options.alt),
  647. orientation: {
  648. heading: Cesium.Math.toRadians(options.heading, 0),
  649. pitch: Cesium.Math.toRadians(options.pitch),
  650. roll: Cesium.Math.toRadians(options.roll),
  651. },
  652. duration: 3,
  653. })
  654. },
  655. /**
  656. * 获取相机视图参数
  657. * @return {JSON} 相机视角参数{lng:lat:alt:heading:pitch:roll}
  658. */
  659. getCameraViewParams: function() {
  660. var _self = this;
  661. /* 获取相机位置 并转换为g84格式 */
  662. var g84Position = Cesium.Ellipsoid.WGS84.cartesianToCartographic(this._viewer.camera.position);
  663. var result = {
  664. lng: Cesium.Math.toDegrees(g84Position.longitude),
  665. lat: Cesium.Math.toDegrees(g84Position.latitude),
  666. alt: g84Position.height,
  667. pitch: Cesium.Math.toDegrees(_self._viewer.camera.pitch),
  668. heading: Cesium.Math.toDegrees(_self._viewer.camera.heading),
  669. roll: Cesium.Math.toDegrees(_self._viewer.camera.roll),
  670. }
  671. return result;
  672. },
  673. /**
  674. * 获取视图
  675. */
  676. getViewer: function() {
  677. return this._viewer;
  678. }
  679. });
  680. /**
  681. * ArcGIS扩展相关
  682. */
  683. Object.assign(CrMap.prototype, {
  684. /**
  685. * 查询ArcGIS服务图层的范围
  686. * @param {String} url 图层服务地址
  687. * @param {Int} index 图层索引Id
  688. * @param {Function} callSuccess 成功回调
  689. * @param {Function} callError
  690. */
  691. _queryAGServerExtent: function(url, index, callSuccess, callError) {
  692. $.ajax({
  693. async: true,
  694. type: 'POST',
  695. dataType: 'json',
  696. url: url + '/' + index + '?f=pjson',
  697. success: function(data) {
  698. if (callSuccess != undefined) {
  699. callSuccess({
  700. xmin: data.extent.xmin,
  701. ymin: data.extent.ymin,
  702. xmax: data.extent.xmax,
  703. ymax: data.extent.ymax,
  704. });
  705. }
  706. },
  707. error: function(XMLHttpRequest, textStatus, errorThrown) {
  708. if (callError != undefined) {
  709. callError(textStatus);
  710. }
  711. }
  712. });
  713. },
  714. /**
  715. * 查询ArcGIS服务图层的范围
  716. * @param {String} url 图层服务地址
  717. * @param {Int} index 图层索引Id
  718. * @param {Function} callSuccess 成功回调
  719. * @param {Function} callError
  720. */
  721. queryAGServerExtent: function(url, index, callSuccess, callError) {
  722. this._queryAGServerExtent(url, index, callSuccess, callError)
  723. }
  724. })
  725. /**
  726. * 图层管理扩展
  727. */
  728. Object.assign(CrMap.prototype, {
  729. /**
  730. * 根据图层配置信息显示图层
  731. * @param {JSON} options 配置项
  732. * @param {JSON} options.config 图层配置信息
  733. */
  734. _showLayer(options) {
  735. console.log('当前加载的图层类型', options.layType);
  736. if (options.layType == CrMap.LayerType.mapboxLayer) {
  737. this.addMapboxLayer(options.config);
  738. } else if (options.layType == CrMap.LayerType.wmtsLayer) {
  739. this.addWmtsTileLayer(options.config);
  740. } else if (options.layType == CrMap.LayerType.imageLayer) {
  741. this.addDynamicFeatureLayerFromServer(options.config);
  742. } else if (options.layType == CrMap.LayerType.tilesetsLayer) {
  743. this.add3DTilesets(options.config);
  744. } else if (options.layType == CrMap.LayerType.agsVectorLayer) {
  745. this.addAGSVectorLayer(options.config);
  746. } else if (options.layType === CrMap.LayerType.templateLayer) {
  747. this.addUrlTemplateImageryLayer(options.config);
  748. } else if (options.layType === CrMap.LayerType.floatLayer) {
  749. this.addFloatLayer(options.config);
  750. }
  751. },
  752. /**
  753. * 设置地形
  754. * @param {JSON} options 配置项
  755. * options.url{string}:地形服务URL地址
  756. */
  757. setTerrain(options) {
  758. var terrainLayer = new Cesium.CesiumTerrainProvider({
  759. url: options.url, //dem的url地址
  760. requestVertexNormals: true, // 请求照明
  761. requestWaterMask: true // 请求水波纹效果
  762. });
  763. this._viewer.terrainProvider = terrainLayer;
  764. },
  765. /**
  766. * 添加图层到图层集合中
  767. * @param {JSON} options 配置项
  768. * @param {String} options.layId 图层Id,唯一标识
  769. * @param {String} options.layName 图层名称
  770. * @param {Enum} options.layType 图层类型
  771. * @param {Boolean} options.isShow 图层是否显示
  772. * @param {JSON} options.config 图层配置信息,不同类型图层配置信息不一致
  773. * @param {Function} callSuccess 成功回调
  774. * @param {Function} callError 错误回调
  775. */
  776. addLayer(options, callSuccess, callError) {
  777. /* 判断用于存储图层的数组是否已经初始化,如未初始化,则进行初始化操作 */
  778. if (!this.allLayers) this.allLayers = [];
  779. if (!options.config) {
  780. /* 如果图层没有添加配置信息 则追加 */
  781. options.config = {
  782. id: options.layId,
  783. }
  784. } else {
  785. /* 如果已经配置了config信息,则增加id字段 */
  786. options.config.id = options.layId;
  787. }
  788. this.allLayers.push(options);
  789. },
  790. /**
  791. * 初始化显示
  792. */
  793. showInit() {
  794. for (let layConfig of this.allLayers) {
  795. if (!layConfig.isShow || layConfig.isShow == false) continue;
  796. this._showLayer(layConfig);
  797. }
  798. },
  799. /**
  800. * 添加Mapbox图层
  801. * @param {JSON} options 配置项
  802. * options.id{String}:可选参数 图层Id
  803. * options.isShow{boolean}[true/false]:是否显示
  804. */
  805. addMapboxLayer(options) {
  806. // var mapboxProvider = new Cesium.MapboxStyleImageryProvider({
  807. // url: 'https://api.mapbox.com/styles/v1',
  808. // username: 'chenchen1990',
  809. // styleId: 'ckvgc14xrh7mo14qowdw4wrkf',
  810. // accessToken: 'pk.eyJ1IjoiY2hlbmNoZW4xOTkwIiwiYSI6ImNrbzA3eTY1OTA3dXkyd20zdG40ZGdmNXYifQ.xWKxjBG6mEDh55_oln0nAg',
  811. // scaleFactor: true
  812. // });
  813. let provider = new ArcgisImageryProvider({
  814. url: 'https://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer',
  815. }, 'WGS84')
  816. /* 加入图层 */
  817. this._addImageryProvider(provider, options.id);
  818. },
  819. /**
  820. * 添加实景数据图层
  821. * @param {JSON} options 配置项
  822. * @param {Array} options.url [id1,id2...]资源的Url数组
  823. * @param {String} options.id 图层Id
  824. * @param {JSON} options.params 参数配置项,可选参数
  825. * @param {JSON} options.params.tx x方向平移
  826. * @param {JSON} options.params.ty y方向平移
  827. * @param {JSON} options.params.tz z方向平移
  828. * @param {JSON} options.params.rx x方向旋转
  829. * @param {JSON} options.params.ry y方向旋转
  830. * @param {JSON} options.params.rz z方向旋转
  831. */
  832. add3DTilesets: function(options) {
  833. let _self = this;
  834. let subLayerIds = [];
  835. let tilesets = new Cesium.PrimitiveCollection();
  836. for (let url of options.url) {
  837. let tileLayer = new Cesium.Cesium3DTileset({
  838. url: url,
  839. maximumScreenSpaceError: 64,
  840. // maxmumMemoryUsage: 218,
  841. // skipScreenSpaceErrorFactor: 16,
  842. // dynamicScreenSpaceError: true,
  843. // skipLevels: 1,
  844. // cullWithChildrenBounds: true,
  845. // cullRequestsWhileMoving: true,
  846. // cullRequestsWhileMovingMultiplier: 10, // 值越小能够更快的剔除
  847. skipLevelOfDetail: true,
  848. maximumScreenSpaceError: 16,
  849. visualError: 60, //可视误差
  850. });
  851. let tileset = tilesets.add(tileLayer);
  852. tileset.readyPromise.then(function(primitive) {
  853. if (options.params != undefined) {
  854. _self._update3dtilesMaxtrix(primitive, options.params);
  855. }
  856. if (options && options.offsetHeight) {
  857. /* 提升高度试试 */
  858. let boundingSphere = tileset.boundingSphere;
  859. let cartographic = Cesium.Cartographic.fromCartesian(boundingSphere.center);
  860. let surface = Cesium.Cartesian3.fromRadians(cartographic.longitude,
  861. cartographic
  862. .latitude, 0.0);
  863. let offseth = Cesium.Cartesian3.fromRadians(cartographic.longitude,
  864. cartographic
  865. .latitude, options.offsetHeight); //修改模型高度
  866. let translation = Cesium.Cartesian3.subtract(offseth, surface, new Cesium
  867. .Cartesian3());
  868. tileset.modelMatrix = Cesium.Matrix4.fromTranslation(translation);
  869. }
  870. })
  871. }
  872. /* 加入图层 */
  873. _self._primitives.add(tilesets);
  874. /* 加入公共资源 */
  875. window[options.id] = tilesets;
  876. },
  877. /**
  878. * 加载Wmts服务数据
  879. * @param {JSON} options 配置项
  880. * @param {String} options.url wmts服务地址
  881. * @param {Number} options.maxLevel 最大缩放层级,默认19
  882. * @param {Number} options.minLevel 最小缩放层级,默认0
  883. * @param {Number} options.alpha[0.0-1.0]:透明度
  884. */
  885. addWmtsTileLayer(options) {
  886. /* 初始化图层 */
  887. var wmtsProvider = new Cesium.WebMapTileServiceImageryProvider({
  888. url: options.url,
  889. layer: 1,
  890. style: 'default',
  891. format: 'image/png',
  892. tileMatrixSetID: 'default028mm',
  893. maximumLevel: options.maxLevel ? options.maxLevel : 19,
  894. minimumLevel: options.minLevel ? options.maxLevel : 0,
  895. });
  896. /* 添加图层 */
  897. var wmtslayer = this._addImageryProvider(wmtsProvider, options.id);
  898. /* 设置透明度 */
  899. wmtslayer.alpha = options.alpha == undefined ? 1.0 : parseFloat(options.alpha);
  900. },
  901. /**
  902. * 加载Tms服务数据
  903. * @param {Object} options 配置项
  904. */
  905. addUrlTemplateImageryLayer: function(options, callSuccess) {
  906. console.log('模版地图参数', JSON.stringify(options));
  907. /* 判断需要的参数是否已经定义 */
  908. if (!Cesium.defined(options) || !Cesium.defined(options.url)) {
  909. throw new DeveloperError("options.url 参数异常!");
  910. }
  911. if (!Cesium.defined(options) || !Cesium.defined(options.maximumLevel)) {
  912. throw new DeveloperError("options.maximumLevel 参数异常!");
  913. }
  914. var imageryProvider = new Cesium.UrlTemplateImageryProvider({
  915. url: options.url,
  916. tilingScheme: new Cesium.WebMercatorTilingScheme(),
  917. minimumLevel: Cesium.defaultValue(options.minimumLevel, 0),
  918. maximumLevel: options.maximumLevel,
  919. })
  920. /* 加入图层 */
  921. let tmsLayer = this._addImageryProvider(imageryProvider);
  922. if (callSuccess) callSuccess(serviceId);
  923. },
  924. /**
  925. * 添加浮动图层 悬浮于实景三维之上
  926. * @param {Object} options
  927. * @param {Object} callSuccess
  928. */
  929. addFloatLayer(options, callSuccess) {
  930. let layer = new CrImageServerLayer({
  931. viewer: this._viewer,
  932. url: options.url,
  933. opacity: options.opacity,
  934. show: true,
  935. });
  936. window[options.id] = layer;
  937. }
  938. })
  939. /**
  940. * 坐标转换相关
  941. */
  942. Object.assign(CrMap.prototype, {
  943. /**
  944. * 地图坐标转三维坐标
  945. * @param {JSON} geo 地理坐标
  946. * @param {Number} geo.lng 经度
  947. * @param {Number} geo.lat 纬度
  948. * @param {Number} geo.height 高度
  949. */
  950. _GeoToCartesian(geo) {
  951. try {
  952. let cartesian = Cesium.Cartesian3.fromDegrees(geo.lng, geo.lat, geo.height);
  953. return cartesian;
  954. } catch (e) {
  955. console.log('错误', e);
  956. return undefined;
  957. }
  958. },
  959. });
  960. /* 扩展全局方法 */
  961. /* 图层类型 */
  962. CrMap.LayerType = Object.freeze({
  963. mapboxLayer: 'mapboxLayer', //Mapbox图层 暗夜版地图
  964. wmtsLayer: 'wmtsLayer', //wmts图层
  965. agsVectorLayer: 'agsVectorLayer', //ArcGIS矢量服务图层
  966. tilesetsLayer: 'tilesetsLayer', //3D Tile图层
  967. glbsLayer: 'glbsLayer', //glb图层
  968. geoJsonLayer: 'geoJsonLayer', //geoJson 类型图层
  969. imageLayer: 'imageLayer', //叠加三维的图片图层
  970. templateLayer: 'templateLayer', //模版类型图层
  971. floatLayer: 'floatLayer', //浮动图层
  972. })
  973. /* 输出 */
  974. export {
  975. CrMap
  976. }