• Cesium点位弹窗


    1.弹窗没法向加入点位一样加入到地图内部,entity没法实现

    2.使用绝对定位,将地图组件通过定位加入到地图上,注意弹窗层级一定在地图上

    3.通过判断点击位置是否是点位来获取entity信息,将信息显示在弹窗

    4.将点击处点位的经纬度转为浏览器视图的x,y坐标,设置弹窗位置

    5.监听地图的缩放和拖动,实时改变显示的弹窗的位置,使弹窗一直保持在点位上方

    效果:

    地图组件(.vue)

    1. <script lang="ts" setup>
    2. import { onMounted } from 'vue';
    3. import { mountedEvt } from './hooks';
    4. import PopUp from './components/PopUp/index.vue';
    5. onMounted(() => {
    6. mountedEvt();
    7. });
    8. script>
    9. <style lang="scss" scoped>
    10. .e {
    11. &-cesium {
    12. height: 100%;
    13. width: 100%;
    14. box-sizing: border-box;
    15. }
    16. }
    17. style>

     hooks.ts文件

    1. import * as Cesium from 'cesium';
    2. import { popInfo } from './config';
    3. let viewer;
    4. export function mountedEvt() {
    5. Cesium.Ion.defaultAccessToken =
    6. '自己的token';
    7. viewer = new Cesium.Viewer('cesiumContainer', {
    8. baseLayerPicker: false, // 关闭图层选择
    9. });
    10. let data = viewer.dataSources.add(
    11. Cesium.GeoJsonDataSource.load('/public/testData/pointLitter.json', {}), // 加载点
    12. );
    13. data.then((dataSource) => {
    14. const entities = dataSource.entities.values;
    15. for (const item in entities) {
    16. const entity = entities[item];
    17. entity.billboard = {
    18. image: '/public/images/gg.png', // 点位图片
    19. color: Cesium.Color.PINK,
    20. width: 40,
    21. height: 40,
    22. heightReference: Cesium.HeightReference.CLAMP_TO_GROUND, // 贴地
    23. };
    24. entity.label = {
    25. text: entity.name, // 标签
    26. font: '16px',
    27. pixelOffset: new Cesium.Cartesian3(0, 30, 0),
    28. };
    29. }
    30. });
    31. viewer.zoomTo(data);
    32. addPopEvt();
    33. }
    34. /**
    35. * @Description 弹窗
    36. * @Author: wms
    37. * @Date: 2023-11-17 11:02:33
    38. */
    39. export const addPopEvt = () => {
    40. let popBox = new Cesium.InfoBox(document.getElementById('popBox'));
    41. viewer.screenSpaceEventHandler.setInputAction(function onLeftClick(
    42. movement,
    43. ) {
    44. let pickedObject = viewer.scene.pick(movement.position);
    45. if (
    46. Cesium.defined(pickedObject) &&
    47. pickedObject.id instanceof Cesium.Entity
    48. ) {
    49. var entity = pickedObject.id;
    50. if (entity.position) {
    51. // 显示弹窗
    52. popBox.container.style.visibility = 'visible';
    53. // 获取位置信息
    54. let entityPosition = entity.position.getValue(
    55. viewer.clock.currentTime,
    56. );
    57. popInfo.value = entity.properties;
    58. // 监听 Viewer 的 postRender 事件,在地图移动时更新弹窗位置
    59. viewer.scene.postRender.addEventListener(function () {
    60. try {
    61. if (entityPosition !== null) {
    62. let screenPosition =
    63. Cesium.SceneTransforms.wgs84ToWindowCoordinates(
    64. viewer.scene,
    65. entityPosition,
    66. );
    67. if (screenPosition) {
    68. let leftOffset =
    69. screenPosition.x -
    70. popBox.container.clientWidth / 2; // 左右位置
    71. let topOffset =
    72. screenPosition.y -
    73. popBox.container.clientHeight -
    74. 18; // 上下位置
    75. popBox.container.style.left = leftOffset + 'px';
    76. popBox.container.style.top = topOffset + 'px';
    77. }
    78. }
    79. } catch (error) {
    80. console.log(error);
    81. }
    82. });
    83. } else {
    84. popBox.container.style.visibility = 'hidden';
    85. }
    86. } else {
    87. // 隐藏弹窗
    88. popBox.container.style.visibility = 'hidden';
    89. }
    90. }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
    91. };

    config.ts文件

    1. import { Ref, ref } from 'vue';
    2. export const popInfo: Ref<any> = ref({});

     弹窗组件(components/PopUp/index.vue)

    1. <script lang="ts" setup>
    2. import { popInfo } from '../../config';
    3. import './index.scss';
    4. script>
    5. <style lang="scss" scoped>style>

    弹窗样式(components/PopUp/index.scss)

    1. .pop {
    2. position: absolute;
    3. display: flex;
    4. flex-direction: column;
    5. width: 280px;
    6. z-index: 99;
    7. visibility: hidden;
    8. &-box {
    9. background-color: rgba(29, 54, 68, 0.8);
    10. &-title {
    11. font-size: 18px;
    12. color: #fff;
    13. padding: 12px;
    14. text-align: center;
    15. color: #fff;
    16. }
    17. &-line {
    18. background-color: #0d1536a9;
    19. height: 1px;
    20. }
    21. &-content {
    22. color: #fff;
    23. padding: 12px;
    24. font-size: 14px;
    25. &-item {
    26. &-labelCont {
    27. color: #fff;
    28. }
    29. }
    30. }
    31. &-triangle {
    32. align-self: center;
    33. width: 0;
    34. height: 0;
    35. border-top: 15px solid rgba(29, 54, 68, 0.8);
    36. border-right: 12px solid transparent;
    37. border-left: 12px solid transparent;
    38. }
    39. }
    40. }

     geojson数据(测试数据  pointLitter.json)

    1. {
    2. "type": "FeatureCollection",
    3. "features": [
    4. {
    5. "type": "Feature",
    6. "geometry": {
    7. "type": "Point",
    8. "coordinates": [
    9. 114,
    10. 30
    11. ]
    12. },
    13. "properties": {
    14. "title": "99",
    15. "color": "#B9EB14",
    16. "symbol":"风格独特"
    17. }
    18. },
    19. {
    20. "type": "Feature",
    21. "geometry": {
    22. "type": "Point",
    23. "coordinates": [
    24. 114.001,
    25. 30
    26. ]
    27. },
    28. "properties": {
    29. "title": "0",
    30. "symbol": "海角天涯",
    31. "color": "#D13C3C"
    32. }
    33. },
    34. {
    35. "type": "Feature",
    36. "geometry": {
    37. "type": "Point",
    38. "coordinates": [
    39. 114.002,
    40. 30
    41. ]
    42. },
    43. "properties": {
    44. "title": "8",
    45. "symbol": "特别的晚风",
    46. "marker-size":12,
    47. "color": "#C49D22"
    48. }
    49. },
    50. {
    51. "type": "Feature",
    52. "geometry": {
    53. "type": "Point",
    54. "coordinates": [
    55. 114.003,
    56. 30
    57. ]
    58. },
    59. "properties": {
    60. "title": "2",
    61. "symbol": "那年仲夏你背上行囊离开家古道旁我欲语泪先下庙里求签我哭诉青梅等竹马",
    62. "color": "#8EE3A6"
    63. }
    64. },
    65. {
    66. "type": "Feature",
    67. "geometry": {
    68. "type": "Point",
    69. "coordinates": [
    70. 114.004,
    71. 30
    72. ]
    73. },
    74. "properties": {
    75. "title": "3",
    76. "symbol": "似水中月情迷着镜中花竹篱笆木琵琶拱桥月下谁在弹唱思念远方牵挂",
    77. "color": "#34BE96"
    78. }
    79. }
    80. ]
    81. }
  • 相关阅读:
    ivew button 使用自定义图标
    2023高教社杯 国赛数学建模A题思路 - 定日镜场的优化设计
    进程通信(1) ----- 无名管道和有名管道
    物联网智慧种植农业大棚系统
    利用cpolar为群晖NAS建立稳定外网地址(1)
    会计学原理知识点总结
    在unity中利用公开变量引用物体和组件(有手就会)
    古人的名与字、号、讳、谥有什么区别
    前端框架Vue学习 ——(六)Vue组件库Element
    AUTOSAR C++14 编码指南(上)
  • 原文地址:https://blog.csdn.net/taozi8957/article/details/134512651