需求:vue项目中使用Cesium,右侧一个标记图标,点击即可在地球任意位置做标记

时间: 2024-01-22 11:21:05 浏览: 19
首先需要在 Vue 项目中安装 Cesium,可以通过以下命令进行安装: ``` npm install cesium --save ``` 然后在需要使用 Cesium 的组件中引入 Cesium: ```javascript import * as Cesium from 'cesium/Cesium' import 'cesium/Widgets/widgets.css' ``` 接着,在组件的 `mounted` 钩子函数中创建 CesiumViewer: ```javascript mounted() { // 创建Cesium Viewer this.viewer = new Cesium.Viewer(this.$refs.cesiumContainer, { animation: false, baseLayerPicker: false, fullscreenButton: false, geocoder: false, homeButton: false, infoBox: false, sceneModePicker: false, selectionIndicator: false, timeline: false, navigationHelpButton: false, navigationInstructionsInitiallyVisible: false, terrainProvider: Cesium.createWorldTerrain() }) } ``` 其中,`this.$refs.cesiumContainer` 是一个 DOM 元素,代表了 Cesium 的容器。 接下来,我们可以在组件中添加一个标记图标,并且添加点击事件: ```html <template> <div class="cesium-container" ref="cesiumContainer"></div> <div class="marker" @click="addMarker"></div> </template> <style> .cesium-container { height: 500px; } .marker { position: absolute; right: 20px; top: 20px; width: 50px; height: 50px; background-image: url(./marker.png); cursor: pointer; } </style> ``` 在 `addMarker` 函数中,我们可以使用 Cesium 的 `pickPosition` 方法获取鼠标点击位置的经纬度,并且在地球上添加一个标记: ```javascript methods: { addMarker() { const handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas) // 点击事件处理 handler.setInputAction((click) => { const cartesian = this.viewer.camera.pickEllipsoid(click.position, this.viewer.scene.globe.ellipsoid) if (cartesian) { // 获取经纬度 const cartographic = Cesium.Cartographic.fromCartesian(cartesian) const longitude = Cesium.Math.toDegrees(cartographic.longitude) const latitude = Cesium.Math.toDegrees(cartographic.latitude) // 创建标记 const marker = this.viewer.entities.add({ position: Cesium.Cartesian3.fromDegrees(longitude, latitude), point: { pixelSize: 10, color: Cesium.Color.YELLOW } }) // 销毁事件处理 handler.destroy() } }, Cesium.ScreenSpaceEventType.LEFT_CLICK) } } ``` 最后,在组件的 `beforeDestroy` 钩子函数中销毁 CesiumViewer: ```javascript beforeDestroy() { if (this.viewer) { this.viewer.destroy() } } ``` 完整的代码如下: ```html <template> <div class="cesium-container" ref="cesiumContainer"></div> <div class="marker" @click="addMarker"></div> </template> <script> import * as Cesium from 'cesium/Cesium' import 'cesium/Widgets/widgets.css' export default { data() { return { viewer: null } }, mounted() { // 创建Cesium Viewer this.viewer = new Cesium.Viewer(this.$refs.cesiumContainer, { animation: false, baseLayerPicker: false, fullscreenButton: false, geocoder: false, homeButton: false, infoBox: false, sceneModePicker: false, selectionIndicator: false, timeline: false, navigationHelpButton: false, navigationInstructionsInitiallyVisible: false, terrainProvider: Cesium.createWorldTerrain() }) }, methods: { addMarker() { const handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas) // 点击事件处理 handler.setInputAction((click) => { const cartesian = this.viewer.camera.pickEllipsoid(click.position, this.viewer.scene.globe.ellipsoid) if (cartesian) { // 获取经纬度 const cartographic = Cesium.Cartographic.fromCartesian(cartesian) const longitude = Cesium.Math.toDegrees(cartographic.longitude) const latitude = Cesium.Math.toDegrees(cartographic.latitude) // 创建标记 const marker = this.viewer.entities.add({ position: Cesium.Cartesian3.fromDegrees(longitude, latitude), point: { pixelSize: 10, color: Cesium.Color.YELLOW } }) // 销毁事件处理 handler.destroy() } }, Cesium.ScreenSpaceEventType.LEFT_CLICK) } }, beforeDestroy() { if (this.viewer) { this.viewer.destroy() } } } </script> <style> .cesium-container { height: 500px; } .marker { position: absolute; right: 20px; top: 20px; width: 50px; height: 50px; background-image: url(./marker.png); cursor: pointer; } </style> ```

相关推荐

最新推荐

recommend-type

详解vue项目中调用百度地图API使用方法

主要介绍了vue项目中调用百度地图API使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

vue项目中使用ueditor的实例讲解

下面小编就为大家分享一篇vue项目中使用ueditor的实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

vue项目中使用fetch的实现方法

主要介绍了vue项目中使用fetch的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

vue项目中使用particles实现粒子背景效果及遇到的坑(按钮没有点击响应)

为了提高页面展示效果,登录界面内容比较单一的,粒子效果作为背景经常使用到,vue工程中利用vue-particles可以很简单的实现页面的粒子背景效果。 解决问题: 以背景方式显示 无法获取按钮焦点,触发不了点击事件 ...
recommend-type

vue项目中使用天地图

2.创建自己的vue项目,这里就不说了 3.将 引入到你项目中的index.html文件中 4.创建map.vue文件 &lt;!-- 点击画多边形 --&gt; export default { data(){ return{ } }, created(){ }, mounted(){ ...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。