Map.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. import weizi from "./data.js"
  2. class Map{
  3. constructor(){
  4. Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI1OTY1NTk0My04OTliLTRlYzAtYWNlNC0yOGNkZWZlMjgxNmIiLCJpZCI6NTE3ODIsImlhdCI6MTYxODMwMzE5OX0.iY_pexbzlJVWZBi9hglcPZnOejagrHmwB2Zbk4uHBec'
  5. //加载在线地形图数据
  6. this._worldTerrain = Cesium.createWorldTerrain({
  7. // required for water effects
  8. requestWaterMask : true,
  9. // required for terrain lighting
  10. requestVertexNormals : true
  11. });
  12. // 服务负载子域
  13. let subdomains = ['0', '1', '2', '3', '4', '5', '6', '7',"8","9"];
  14. let tiandituTk = "ac8f435fa9098eda8c1f40ba97b0b0f2";
  15. let provider = new Cesium.WebMapTileServiceImageryProvider({
  16. //影像底图
  17. url: "http://t0.tianditu.gov.cn/img_w/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=img&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&style=default&format=tiles&tk=" + tiandituTk,
  18. subdomains: subdomains,
  19. layer: "img",
  20. style: "default",
  21. format: "image/jpeg",
  22. tileMatrixSetID: "GoogleMapsCompatible", //使用谷歌的瓦片切片方式
  23. show: true
  24. });
  25. this._viewer = new Cesium.Viewer('container', {
  26. shouldAnimate: true,
  27. infoBox: false, //是否显示点击要素之后显示的信息
  28. selectionIndicator:false,
  29. fullscreenButton: false,
  30. orderIndependentTranslucency: false,
  31. homeButton:false,
  32. sceneModePicker:false,
  33. baseLayerPicker:false,
  34. navigationHelpButton:false ,
  35. vrButton:false,
  36. geocoder: false, //是否显示地名查找控件
  37. timeline: false, //是否显示时间线控件
  38. animation:false,
  39. imageryProvider:provider,
  40. terrainProvider: this._worldTerrain,
  41. });
  42. this._viewer.scene.backgroundColor =new Cesium.Color(0,7/255,25/255);
  43. this._viewer._cesiumWidget._creditContainer.style.display = "none";
  44. this._viewer.scene.skyAtmosphere.show = false;
  45. this._viewer.scene.globe.showGroundAtmosphere=false;
  46. this._viewer.scene.globe.showWaterEffect=false;
  47. this._viewer.scene.fog.enabled = false;
  48. this._viewer.scene.skyBox.show=false;
  49. this._viewer.shadowMap.darkness=0.2;
  50. this._viewer.isPointLight=true;
  51. //是否开启抗锯齿
  52. this._viewer.scene.fxaa = true;
  53. this._viewer.scene.postProcessStages.fxaa.enabled = true;
  54. this._tileset1 = new Cesium.Cesium3DTileset({
  55. url: 'https://testgis.4dage.com/jiujiang-xiugai1-qp/tileset.json',
  56. });
  57. let tileset=this._tileset1;
  58. this._tileset1.readyPromise.then(function () {
  59. let boundingSphere = tileset.boundingSphere;
  60. let cartographic = Cesium.Cartographic.fromCartesian(boundingSphere.center);
  61. let surface = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, cartographic.height);
  62. let offset = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, cartographic.height + 20);
  63. let translation = Cesium.Cartesian3.subtract(offset, surface, new Cesium.Cartesian3());
  64. tileset.modelMatrix = Cesium.Matrix4.fromTranslation(translation);
  65. })
  66. this._viewer.scene.primitives.add(this._tileset1);
  67. this._viewer.camera.flyTo({
  68. destination:Cesium.Cartesian3.fromDegrees(116.21584100853136,29.738977165300618,243.68175872316817),
  69. orientation:{
  70. heading:0.05766205214782261,
  71. pitch:-0.46911279010391693,
  72. roll:0.000005273011606909961
  73. },
  74. duration:1
  75. });
  76. // 亮度设置
  77. var stages = this._viewer.scene.postProcessStages;
  78. this._viewer.scene.brightness = this._viewer.scene.brightness || stages.add(Cesium.PostProcessStageLibrary.createBrightnessStage());
  79. this._viewer.scene.brightness.enabled = true;
  80. this._viewer.scene.brightness.uniforms.brightness = Number(1.0);
  81. this._handler = new Cesium.ScreenSpaceEventHandler(this._viewer.scene.canvas);
  82. let viewer=this._viewer;
  83. this._handler.setInputAction(function (movement) {
  84. var obj = viewer.scene.pick(movement.endPosition);
  85. viewer.container.style.cursor = "default";
  86. if(obj&&obj.id&&obj.id.label._text=="金湖国际小区"){
  87. viewer.container.style.cursor = "pointer";
  88. }
  89. if(obj&&obj.id&&obj.id.label._text=="老县委宿舍"){
  90. viewer.container.style.cursor = "pointer";
  91. }
  92. if(obj&&obj.id&&obj.id.label._text=="1#"){
  93. viewer.container.style.cursor = "pointer";
  94. }
  95. if(obj&&obj.id&&obj.id.label._text=="叶家舍小区"){
  96. viewer.container.style.cursor = "pointer";
  97. }
  98. }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  99. this._heilayer=new Cesium.ImageryLayer(new Cesium.UrlTemplateImageryProvider({
  100. url: "https://map.geoq.cn/arcgis/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}"
  101. }));
  102. this._viewer.imageryLayers.add(this._heilayer)
  103. this._heilayer.show=false;
  104. this.name=null;
  105. }
  106. left_clickFun(clickjinghuguojiShowFun,jinghuguojiShowFun,laoxianweisusheShowFun,yejiaxiaosheShowFun){
  107. let viewer=this._viewer;
  108. this._handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)
  109. this._handler.setInputAction(function (movement) {
  110. var obj = viewer.scene.pick(movement.position);
  111. if(obj&&obj.id&&obj.id.label._text=="金湖国际小区"){
  112. viewer.camera.flyTo({
  113. destination:Cesium.Cartesian3.fromDegrees(116.21593231048116,29.740258229946367,162.62117710327865),
  114. orientation:{
  115. heading: 0.05766205214782261,
  116. pitch:-0.46911279010391693,
  117. roll:0.000005273011606909961
  118. },
  119. duration:1
  120. });
  121. clickjinghuguojiShowFun()
  122. }
  123. if(obj&&obj.id&&obj.id.label._text=="老县委宿舍"){
  124. viewer.camera.flyTo({
  125. destination:Cesium.Cartesian3.fromDegrees(116.21644159026744,29.741029388219182,121.07007997440711),
  126. orientation:{
  127. heading: 0.05766205214782261,
  128. pitch:-0.46911279010391693,
  129. roll:0.000005273011606909961
  130. },
  131. duration:1
  132. });
  133. }
  134. if(obj&&obj.id&&obj.id.type=="jinghuguoji"&&obj.id.label._text=="1#"){
  135. jinghuguojiShowFun()
  136. }
  137. if(obj&&obj.id&&obj.id.type=="laoxianweisushe"&&obj.id.label._text=="1#"){
  138. laoxianweisusheShowFun()
  139. }
  140. if(obj&&obj.id&&obj.id.label._text=="叶家舍小区"){
  141. yejiaxiaosheShowFun()
  142. }
  143. }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  144. }
  145. adddEntity1(position,str,background){
  146. this._viewer.entities.add({
  147. position:position,
  148. label:{
  149. text:str,
  150. showBackground:true,
  151. backgroundColor:background,
  152. pixelOffset:new Cesium.Cartesian2(0,-140),
  153. scale:0.5
  154. },
  155. billboard:{
  156. image:"../png/triangle.png",
  157. pixelOffset:new Cesium.Cartesian2(0,-125),
  158. scale:0.5
  159. }
  160. });
  161. }
  162. adddEntity2(position,str,borderColor){
  163. this._viewer.entities.add({
  164. type:str.split("_")[0],
  165. position:position,
  166. label:{
  167. text:str.split("_")[1],
  168. outlineColor:borderColor,
  169. fillColor:Cesium.Color.WHITE,
  170. style:Cesium.LabelStyle.FILL_AND_OUTLINE,
  171. showBackground:true,
  172. backgroundColor:new Cesium.Color(13/255,146/255,246/255,0.1),
  173. outlineWidth:20,
  174. pixelOffset:new Cesium.Cartesian2(0,-120),
  175. scale:0.5
  176. }
  177. });
  178. }
  179. addEntity(){
  180. this.adddEntity1(new Cesium.Cartesian3 (-2448457.0543241943, 4972430.5303345295, 3145603.3914947766) ,"金湖国际小区",new Cesium.Color(13/255,146/255,246/255));
  181. this.adddEntity2(new Cesium.Cartesian3 ( -2448463.0940114185, 4972437.1644585775, 3145587.8985699383),"jinghuguoji_1#",new Cesium.Color(13/255,146/255,246/255));
  182. this.adddEntity2(new Cesium.Cartesian3 ( -2448474.523461309, 4972421.640874091, 3145603.5562153812),"jinghuguoji_2#",new Cesium.Color(13/255,146/255,246/255));
  183. this.adddEntity1(new Cesium.Cartesian3 ( -2448508.4188250797, 4972329.740959639, 3145648.777494665),"老县委宿舍",new Cesium.Color(13/255,146/255,246/255));
  184. this.adddEntity2(new Cesium.Cartesian3 ( -2448497.7840337977, 4972353.026540443, 3145637.2073454955),"laoxianweisushe_1#",new Cesium.Color(13/255,146/255,246/255));
  185. this.adddEntity2(new Cesium.Cartesian3 ( -2448487.6369428034, 4972366.6064854115,3145602.2819039533),"laoxianweisushe_2#",new Cesium.Color(13/255,146/255,246/255));
  186. this.adddEntity2(new Cesium.Cartesian3 ( -2448520.3171395767, 4972369.752162702, 3145592.3613111465),"laoxianweisushe_3#",new Cesium.Color(13/255,146/255,246/255));
  187. }
  188. addLocationName(event){
  189. let name=""
  190. weizi.map(w=>{
  191. if(event.target.id===w.id){
  192. let height=10;
  193. name=w.name
  194. if(w.name=="西门社区")
  195. {
  196. height=50;
  197. this._viewer.camera.flyTo({
  198. destination:Cesium.Cartesian3.fromDegrees(116.21536139714668,29.735710683444914,210.11715193973072),
  199. orientation:{
  200. heading: 5.387296195840215,
  201. pitch: -0.4462147819503275,
  202. roll: 6.283184526554923
  203. },
  204. duration:0.5
  205. });
  206. }else{
  207. this._viewer.camera.flyTo({
  208. destination:Cesium.Cartesian3.fromDegrees(w.lon,w.lat-0.02,1000),
  209. orientation:{
  210. heading: 0.05766205214782261,
  211. pitch:-0.46911279010391693,
  212. roll:0.000005273011606909961
  213. },
  214. duration:0.5
  215. });
  216. }
  217. this._viewer.entities.add({
  218. position:Cesium.Cartesian3.fromDegrees(w.lon,w.lat,height),
  219. label:{
  220. text:w.name,
  221. fillColor:Cesium.Color.WHITE,
  222. showBackground:true,
  223. backgroundColor:new Cesium.Color(13/255,146/255,246/255,0.8),
  224. outlineWidth:20,
  225. scale:1
  226. }
  227. });
  228. }
  229. });
  230. // debugger
  231. return name;
  232. }
  233. addGaode(){
  234. this._tileset1.show=false;
  235. this._viewer.entities.removeAll();
  236. this._viewer.scene.terrainProvider=new Cesium.EllipsoidTerrainProvider({});
  237. this._heilayer.show=true;
  238. }
  239. removeGaode(){
  240. this._tileset1.show=true;
  241. this._viewer.camera.flyTo({
  242. destination:Cesium.Cartesian3.fromDegrees(116.21584100853136,29.738977165300618,243.68175872316817),
  243. orientation:{
  244. heading:0.05766205214782261,
  245. pitch:-0.46911279010391693,
  246. roll:0.000005273011606909961
  247. },
  248. duration:1
  249. });
  250. this._viewer.entities.removeAll();
  251. this.addEntity();
  252. this._viewer.scene.terrainProvider=this._worldTerrain;
  253. this._heilayer.show=false;
  254. }
  255. loadximen(){
  256. this._tileset1.show=true;
  257. this._viewer.camera.flyTo({
  258. destination:Cesium.Cartesian3.fromDegrees(116.219468,29.737767,243.68175872316817),
  259. orientation:{
  260. heading:0.05766205214782261,
  261. pitch:-0.46911279010391693,
  262. roll:0.000005273011606909961
  263. },
  264. duration:1
  265. });
  266. this._viewer.entities.removeAll();
  267. this._viewer.scene.terrainProvider=this._worldTerrain;
  268. this._heilayer.show=false;
  269. }
  270. }
  271. // var handler = new Cesium.ScreenSpaceEventHandler(this._viewer.scene.canvas);
  272. // handler.setInputAction(function (event) {
  273. // //1.椭球面坐标:获取当前点击视线与椭球面相交处的坐标,在加载地形的场景上获取的坐标有误差
  274. // // var earthPosition = map.this._viewer.camera.pickEllipsoid(event.position,map.this._viewer.scene.globe.ellipsoid);
  275. // //2.场景坐标:获取场景中任意点击处的对应的世界坐标,需要开启“地形深度检测”(在未开启“地形深度检测”的情况下只能在3DTile上准确获取空间坐标,开启“地形深度检测”后,this._viewer.scene.pickPosition 也能在非3DTile上准确获取坐标)
  276. // this._viewer.scene.globe.depthTestAgainstTerrain = true;
  277. // var position1 = this._viewer.scene.pickPosition(event.position);
  278. // var ellipsoid = this._viewer.scene.globe.ellipsoid;
  279. // var cartographic = ellipsoid.cartesianToCartographic(position1);
  280. // var lat1 = Cesium.Math.toDegrees(cartographic.latitude);
  281. // var lng1 = Cesium.Math.toDegrees(cartographic.longitude);
  282. // var alt1 = cartographic.height;
  283. // console.log({
  284. // lat1:lat1,
  285. // lng1:lng1,
  286. // alt1:alt1
  287. // })
  288. // var cartographic = ellipsoid.cartesianToCartographic(this._viewer.camera.position);
  289. // var lat = Cesium.Math.toDegrees(cartographic.latitude);
  290. // var lng = Cesium.Math.toDegrees(cartographic.longitude);
  291. // var alt = cartographic.height;
  292. // console.log({
  293. // lat:lat,
  294. // lng:lng,
  295. // alt:alt,
  296. // heading:this._viewer.camera.heading,
  297. // pitch:this._viewer.camera.pitch,
  298. // roll:this._viewer.camera.roll
  299. // })
  300. // }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  301. // function GetWGS84FromDKR(coor)
  302. // {
  303. // let cartographic = Cesium.Cartographic.fromCartesian(coor);
  304. // let x=Cesium.Math.toDegrees(cartographic.longitude);l
  305. // let y=Cesium.Math.toDegrees(cartographic.latitude);
  306. // let wgs84={
  307. // lon:x,
  308. // lat:y,
  309. // };
  310. // return wgs84;
  311. // }
  312. export default Map;