• vue+cesium项目demo


    使用turf时在index.html中引用

    1. <script src="https://unpkg.com/@turf/turf/turf.min.js">script>

    1. <script setup>
    2. import { ErrorCodes, onMounted } from "vue";
    3. import * as Cesium from "cesium";
    4. let viewer, pointDel, dataturf;
    5. function toDelpoint() {
    6. console.log(viewer.entities);
    7. // 直接删除
    8. // viewer.entities.remove(pointDel)
    9. // 删除所有
    10. // viewer.entities.removeAll()
    11. // 删除指定
    12. // viewer.entities.removeById('rectangleimg')
    13. // 先拿到实体再删除
    14. // const entityPoint = viewer.entities.getById("rectangleimg");
    15. // viewer.entities.remove(entityPoint);
    16. viewer.dataSources.remove(dataturf);
    17. }
    18. onMounted(() => {
    19. // 设置cesium token
    20. Cesium.Ion.defaultAccessToken =
    21. "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJhYzY2NGQ0My1hMjQxLTQxZDgtYjQwYy1mZjQ1YjVmYzljZDUiLCJpZCI6MjEzNDUyLCJpYXQiOjE3MTUwNzI2MTR9.ID13prfqYbHG4-WqCl9RF9cymnB3wXi4ckGO1BZvF4Y";
    22. // // 创建自定义图层
    23. // const esri=new Cesium.ArcGisMapServerImageryProvider({
    24. // url: "https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer",
    25. // enablePickFeatures: false,
    26. // })
    27. // // 初始化cesium
    28. // // viewer是所有api的开始
    29. // const viewer = new Cesium.Viewer("cesiumContatiner",{
    30. // imageryProvider:esri, //自定义图层,默认是谷歌的影响图层
    31. // // 地形图层
    32. // terrainProvider:Cesium.createWorldTerrain({
    33. // // requestWaterMask:true //水面特效
    34. // })
    35. // });
    36. // 【控件】
    37. viewer = new Cesium.Viewer("cesiumContatiner", {
    38. timeline: false, // 时间轴
    39. animation: false, // 动画小组件
    40. geocoder: false, // 地理编码(搜索)组件
    41. homeButton: false, // 首页,点击之后将视图跳转到默认视角
    42. sceneModePicker: false, // 投影方式,切换2D、3D 和 Columbus View (CV) 模式。
    43. baseLayerPicker: false, // 底图组件,选择三维数字地球的底图(imagery and terrain)。
    44. navigationHelpButton: false, // 帮助按钮
    45. fullscreenButton: false, // 全屏按钮
    46. // vrButton: false, // VR模式
    47. selectionIndicator: false, //是否显示选取指示器组件
    48. // shouldAnimate: true, // 自动播放动画控件
    49. // shadows: true, // 是否显示光照投射的阴影
    50. // terrainShadows: Cesium.ShadowMode.RECEIVE_ONLY, // 地质接收阴影`
    51. infoBox: true, // 信息框
    52. });
    53. // 隐藏logo
    54. viewer._cesiumWidget._creditContainer.style.display = "none";
    55. // promitive entity
    56. // entity 基于promitive封装的
    57. // 完美封装,调用方便
    58. // promitive
    59. // 更接近webgl底层
    60. // 可以绘制更高级的图形
    61. const tileset = viewer.scene.primitives.add(
    62. new Cesium.Cesium3DTileset({
    63. url: Cesium.IonResource.fromAssetId(69380),
    64. })
    65. );
    66. viewer.flyTo(tileset);
    67. // 线
    68. var linestring1 = turf.lineString([
    69. [-24, 63],
    70. [-23, 60],
    71. [-25, 65],
    72. [-20, 69],
    73. ]);
    74. // Cesium.GeoJsonDataSource.load(linestring1).then(function (dataSource) {
    75. // viewer.dataSources.add(dataSource);
    76. // var entities = dataSource.entities.values;
    77. // for (var i = 0; i < entities.length; i++) {
    78. // var entity = entities[i];
    79. // entity.polyline.width = 2;
    80. // entity.polyline.material = Cesium.Color.RED;
    81. // }
    82. // viewer.zoomTo(dataSource);
    83. // });
    84. Cesium.GeoJsonDataSource.load(linestring1).then((res) => {
    85. console.log(res);
    86. const entity = res.entities.values[0];
    87. entity.polyline.material = Cesium.Color.RED;
    88. entity.polyline.width = 2;
    89. viewer.entities.add(entity);
    90. // viewer.zoomTo(entity)
    91. });
    92. // 多条线
    93. var multiLine = turf.multiLineString([
    94. [
    95. [0, 0],
    96. [5, 5],
    97. ],
    98. [
    99. [6, 6],
    100. [10, 10],
    101. ],
    102. ]);
    103. Cesium.GeoJsonDataSource.load(multiLine).then((res) => {
    104. viewer.dataSources.add(res);
    105. dataturf = res;
    106. // viewer.zoomTo(res)
    107. });
    108. // 注意:polygon首尾坐标要一致
    109. var polygonturf = turf.polygon([
    110. [
    111. [-5, 52],
    112. [-4, 56],
    113. [-2, 51],
    114. [-5, 52],
    115. ],
    116. ]);
    117. const polygonline = Cesium.GeoJsonDataSource.load(polygonturf);
    118. viewer.dataSources.add(polygonline);
    119. // viewer.zoomTo(polygonline);
    120. // 多个多边形
    121. var multiPoly = turf.multiPolygon([
    122. [
    123. [
    124. [0, 0],
    125. [0, 10],
    126. [10, 10],
    127. [10, 0],
    128. [0, 0],
    129. ],
    130. ],
    131. [
    132. [
    133. [12, 12],
    134. [12, 23],
    135. [23, 23],
    136. [23, 12],
    137. [12, 12],
    138. ],
    139. ],
    140. ]);
    141. let multiPolyTurf = Cesium.GeoJsonDataSource.load(multiPoly);
    142. viewer.dataSources.add(multiPolyTurf);
    143. // viewer.zoomTo(multiPolyTurf);
    144. // let poram = Cesium.GeoJsonDataSource.load(
    145. // "../public/ne_10m_us_states.topojson"
    146. // );
    147. // viewer.dataSources.add(poram);
    148. // viewer.zoomTo(poram)
    149. // 【kmz】
    150. // let promise = Cesium.KmlDataSource.load("../public/gdpPerCapita2008.kmz");
    151. // viewer.dataSources.add(promise);
    152. // viewer.zoomTo(promise)
    153. // 【坐标转换】
    154. //经纬度转笛卡尔坐标
    155. // 返回的是一个笛卡尔坐标(z!=高度)
    156. // const cartesian = Cesium.Cartesian3.fromDegrees(110, 20, 20); //经度、纬度、高度
    157. // const cartesian2 = Cesium.Cartesian3.fromDegrees(110, 20, 30); //经度、纬度、高度
    158. // // console.log(cartesian,cartesian2)
    159. // // 1、笛卡尔坐标转经纬度坐标
    160. // let cartographic = Cesium.Cartographic.fromCartesian(cartesian)
    161. // //2、弧度坐标转角度坐标1
    162. // // let lon=180 / Math.PI*cartographic.longitude
    163. // // let lat=180 / Math.PI*cartographic.latitude
    164. // //2、弧度坐标转角度坐标2(cesium自带的方法)
    165. // let lon =Cesium.Math.toDegrees(cartographic.longitude)
    166. // let lat =Cesium.Math.toDegrees(cartographic.latitude)
    167. // let height=cartographic.height
    168. // console.log(lon,lat,height)
    169. // 【相机】
    170. // const postition=Cesium.Cartesian3.fromDegrees(116.397128,39.916527,20000)
    171. // // setView通过定义相机目的地(方向),直接跳转到目的地
    172. // viewer.camera.setView({
    173. // destination:postition,
    174. // // 默认(0,-90,0)
    175. // orientation: {
    176. // heading: Cesium.Math.toRadians(0), // 方向 y 左右摇头
    177. // pitch: Cesium.Math.toRadians(-90), // 倾斜角度 x 上下点头 -90看地面 0看前方 90看天上
    178. // roll: 0, //z 歪头看
    179. // },
    180. // })
    181. // 【相机】【飞行】flyTo 将相机从当前位置移动到新的位置
    182. // flyTo快速切换视角,带飞行动画,可以设置飞行的时长
    183. // const postition=Cesium.Cartesian3.fromDegrees(110,20,20000)
    184. // viewer.camera.flyTo({
    185. // destination: postition,
    186. // duration:3, //单位秒
    187. // orientation: {
    188. // heading: Cesium.Math.toRadians(0), // 方向 y 左右摇头
    189. // pitch: Cesium.Math.toRadians(-90), // 倾斜角度 x 上下点头 -90看地面 0看前方 90看天上
    190. // roll: 0, //z 歪头看
    191. // },
    192. // });
    193. // 【相机】
    194. // lookAt将视角固定在设置的点位上,可以选择视角,但不能改变位置
    195. // const position2=Cesium.Cartesian3.fromDegrees(110,20)
    196. // viewer.camera.lookAt(position2,new Cesium.HeadingPitchRange(
    197. // Cesium.Math.toRadians(0),
    198. // Cesium.Math.toRadians(-90),
    199. // 20000
    200. // )
    201. // )
    202. // 实体 entity
    203. // 写法一
    204. // const point=new Cesium.Entity({
    205. // position:Cesium.Cartesian3.fromDegrees(120,30),
    206. // point:{
    207. // pixelSize:20, //像素大小
    208. // color:Cesium.Color.RED,
    209. // // outlineColor:Cesium.Color.WHITE,
    210. // // outlineWidth:2,
    211. // // heightReference:Cesium.HeightReference.CLAMP_TO_GROUND
    212. // }
    213. // })
    214. // viewer.entities.add(point)
    215. // // console.log(viewer.entities)
    216. // viewer.zoomTo(point)
    217. // 方法二(推荐)
    218. const point2 = viewer.entities.add({
    219. id: "point",
    220. position: Cesium.Cartesian3.fromDegrees(120, 30),
    221. point: {
    222. pixelSize: 20, //像素大小
    223. color: Cesium.Color.RED,
    224. outlineColor: Cesium.Color.WHITE,
    225. outlineWidth: 20,
    226. heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
    227. },
    228. });
    229. // viewer.zoomTo(point2)
    230. // 与该实体相关联的广告牌。
    231. const billboard = viewer.entities.add({
    232. id: "billboard",
    233. position: Cesium.Cartesian3.fromDegrees(116, 40, 10),
    234. billboard: {
    235. image: "../public/dw.png",
    236. scale: 0.3,
    237. color: Cesium.Color.RED,
    238. // width:20,
    239. // height:20
    240. },
    241. });
    242. // 【label】
    243. const label = viewer.entities.add({
    244. id: "label",
    245. position: Cesium.Cartesian3.fromDegrees(112, 30, 4000),
    246. label: {
    247. text: "cesium",
    248. font: "20px sans-serif",
    249. fillColor: Cesium.Color.WHITE,
    250. showBackground: true,
    251. backgroundColor: Cesium.Color.BLACK.withAlpha(0.5),
    252. backgroundPadding: new Cesium.Cartesian2(10, 10),
    253. outlineColor: Cesium.Color.RED,
    254. outlineWidth: 2,
    255. style: Cesium.LabelStyle.FILL_AND_OUTLINE,
    256. horizontalOrigin: Cesium.HorizontalOrigin.RIGHT,
    257. verticalOrigin: Cesium.VerticalOrigin.TOP,
    258. // scaleByDistance: new Cesium.NearFarScalar(1.0e2, 1, 1.0e3, 0.2),//随着距离改变标注尺寸
    259. // pixelOffsetScaleByDistance: new Cesium.NearFarScalar(1.0e2, 1.0, 1.0e3, 0.2), //随着距离改变偏移量
    260. // translucencyByDistance:new Cesium.NearFarScalar(1.5e2,1,8.0e6,0.0),
    261. // pixelOffset:new Cesium.Cartesian2(0,1)
    262. },
    263. });
    264. // viewer.zoomTo(label);
    265. // //线
    266. const polyline = viewer.entities.add({
    267. polyline: {
    268. positions: Cesium.Cartesian3.fromDegreesArray([
    269. 118, 29, 119, 29, 118, 31,
    270. ]), //返回笛卡尔坐标数组
    271. width: 10,
    272. material: Cesium.Color.RED,
    273. },
    274. });
    275. // viewer.zoomTo(polyline)
    276. // 多边形
    277. const polygon = viewer.entities.add({
    278. id: "polygon",
    279. polygon: {
    280. // 左下角、右下角、右上、左上
    281. hierarchy: Cesium.Cartesian3.fromDegreesArray([
    282. 121, 30, 123, 30, 122, 31, 120, 31,
    283. ]),
    284. material: Cesium.Color.RED.withAlpha(0.5),
    285. height: 200000,
    286. extrudedHeight: 10000,
    287. heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
    288. outline: true, //是否展示外线
    289. outlineColor: Cesium.Color.WHITE,
    290. outlineWidth: 2,
    291. fill: true, //是否填充
    292. },
    293. // polygon: {
    294. // hierarchy: {
    295. // positions: Cesium.Cartesian3.fromDegreesArray([
    296. // 121, 30, 123, 30, 122, 31, 120, 31,
    297. // ]),
    298. // },
    299. // material: Cesium.Color.RED.withAlpha(0.5),
    300. // },
    301. });
    302. // viewer.zoomTo(polygon);
    303. // box
    304. const box = viewer.entities.add({
    305. position: Cesium.Cartesian3.fromDegrees(120, 30, 10000),
    306. box: {
    307. dimensions: new Cesium.Cartesian3(40000, 40000, 40000),
    308. material: Cesium.Color.CYAN.withAlpha(0.5),
    309. outline: true,
    310. outlineColor: Cesium.Color.WHITE,
    311. outlineWidth: 2,
    312. },
    313. });
    314. // viewer.zoomTo(box)
    315. // 椭圆
    316. const ellipse = viewer.entities.add({
    317. position: Cesium.Cartesian3.fromDegrees(110, 30),
    318. ellipse: {
    319. semiMinorAxis: 3000, //半短轴
    320. semiMajorAxis: 5000, //半长轴
    321. // material:Cesium.Color.DARKBLUE.withAlpha(0.5),
    322. material: "../public/img.jpeg",
    323. outline: true,
    324. outlineColor: Cesium.Color.RED,
    325. outlineWidth: 20,
    326. rotation: Math.PI / 2, //选择角度
    327. extrudedHeight: 4000,
    328. fill: true,
    329. },
    330. });
    331. // viewer.zoomTo(ellipse)
    332. // 矩形
    333. const rectangle = viewer.entities.add({
    334. id: "rectangleimg",
    335. rectangle: {
    336. coordinates: Cesium.Rectangle.fromDegrees(116, 30, 117, 31),
    337. material: "../public/girl.jpg",
    338. outline: true,
    339. outlineColor: Cesium.Color.WHITE,
    340. outlineWidth: 2,
    341. height: 100000,
    342. extrudedHeight: 100000, //获取或设置数字属性,该属性指定矩形拉伸的高度。设置此属性将创建从高处开始并在此高度处结束的体积。
    343. heightReference: Cesium.HeightReference.CLAMP_TO_GROUND, //获取或设置指定拉伸的 HeightReference 的属性。
    344. fill: true,
    345. },
    346. });
    347. // viewer.zoomTo(rectangle)
    348. // 组合
    349. // const billboardZH = viewer.entities.add({
    350. // position: Cesium.Cartesian3.fromDegrees(116.17860748743055, 39.908307123610385, 100),
    351. // billboard: {
    352. // image: "../public/dw.png",
    353. // scale: 0.3,
    354. // color: Cesium.Color.YELLOW,
    355. // },
    356. // });
    357. // const lineZH = viewer.entities.add({
    358. // polyline: {
    359. // positions: Cesium.Cartesian3.fromDegreesArrayHeights([ 116.17860748743055, 39.908307123610385,0,116.17860748743055,39.908307123610385,100]),
    360. // // width: 5,
    361. // material: Cesium.Color.RED,
    362. // },
    363. // });
    364. // const labelZH=viewer.entities.add({
    365. // position: Cesium.Cartesian3.fromDegrees(116.17860748743055, 39.908307123610385, 100),
    366. // label: {
    367. // text: "中海商务",
    368. // font: "24px sans-serif",
    369. // fillColor: Cesium.Color.RED,
    370. // outlineColor: Cesium.Color.WHITE,
    371. // outlineWidth: 2,
    372. // style: Cesium.LabelStyle.FILL_AND_OUTLINE,
    373. // pixelOffset:
    374. // new Cesium.Cartesian2(0, -50), //偏移量
    375. // // eyeOffset: new Cesium.Cartesian3(0, 0, 1000), //眼睛的偏移量
    376. // },
    377. // })
    378. // viewer.zoomTo(billboardZH);
    379. // 组合实体
    380. const entity = viewer.entities.add({
    381. position: Cesium.Cartesian3.fromDegrees(
    382. 116.17860748743055,
    383. 39.908307123610385,
    384. 100
    385. ),
    386. billboard: {
    387. image: "../public/dw.png",
    388. scale: 0.3,
    389. color: Cesium.Color.YELLOW,
    390. },
    391. polyline: {
    392. positions: Cesium.Cartesian3.fromDegreesArrayHeights([
    393. 116.17860748743055, 39.908307123610385, 0, 116.17860748743055,
    394. 39.908307123610385, 100,
    395. ]),
    396. // width: 5,
    397. material: Cesium.Color.RED,
    398. },
    399. label: {
    400. text: "中海商务",
    401. font: "24px sans-serif",
    402. fillColor: Cesium.Color.RED,
    403. outlineColor: Cesium.Color.WHITE,
    404. outlineWidth: 2,
    405. style: Cesium.LabelStyle.FILL_AND_OUTLINE,
    406. pixelOffset: new Cesium.Cartesian2(0, -50), //偏移量
    407. // eyeOffset: new Cesium.Cartesian3(0, 0, 1000), //眼睛的偏移量
    408. },
    409. });
    410. // viewer.zoomTo(entity);
    411. pointDel = viewer.entities.add({
    412. id: "pointDel",
    413. position: Cesium.Cartesian3.fromDegrees(116.17860748743055, 39.908),
    414. point: {
    415. pixelSize: 50,
    416. color: Cesium.Color.MEDIUMPURPLE,
    417. outlineColor: Cesium.Color.LIME,
    418. outlineWidth: 20,
    419. heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
    420. },
    421. });
    422. // viewer.zoomTo(pointDel);
    423. let lon,
    424. lat,
    425. num = 0;
    426. const lineYD = viewer.entities.add({
    427. polyline: {
    428. positions: new Cesium.CallbackProperty(() => {
    429. console.log(1);
    430. num += 0.005;
    431. lon = 116 + num;
    432. lat = 35 + num;
    433. if (lon < 117) {
    434. return Cesium.Cartesian3.fromDegreesArray([116, 35, lon, lat]);
    435. } else {
    436. // 给positions赋值一个新的对象,不再需要CallbackProprty 提供
    437. lineYD.polyline.positions = Cesium.Cartesian3.fromDegreesArray([
    438. 116,
    439. 35,
    440. lon,
    441. lat,
    442. ]);
    443. }
    444. }, false), //传false,不传动不起来
    445. width: 5,
    446. material: Cesium.Color.RED,
    447. // clampToGround:true
    448. },
    449. });
    450. // viewer.zoomTo(lineYD);
    451. });
    452. script>
    453. <style scoped lang='scss'>
    454. #cesiumContatiner {
    455. width: 100vw;
    456. height: 100vh;
    457. overflow: hidden;
    458. // :deep .cesium-viewer-bottom {
    459. // display: none;
    460. // }
    461. }
    462. .cesium_box {
    463. position: relative;
    464. }
    465. .bthdel_point {
    466. position: absolute;
    467. top: 0;
    468. left: 0;
    469. width: 100px;
    470. text-align: center;
    471. padding: 10px;
    472. background: rgba(0, 0, 0, 0.5);
    473. color: #fff;
    474. border-radius: 5px;
    475. cursor: pointer;
    476. }
    477. style>

  • 相关阅读:
    VSCODE解决git合并过程中的冲突问题;error: failed to push some refs to
    【IAP Kit】应用内支付订单参数相关问题解析
    测试环境搭建整套大数据系统(十四:报错-bash: /root: Is a directory
    HTML开篇之安装VSvode(用记事本编辑HTML)
    python中的range的详解
    6.26CF模拟赛E:价格最大化题解
    软考随记(二)
    C基础day8
    二次开发真的很难吗?低代码平台有话说
    我的设计模式之旅 ⑦ 观察者模式
  • 原文地址:https://blog.csdn.net/weixin_42120669/article/details/138725639