在vue2中使用cesium

准备:首先你需要有一个vue2的项目,然后有一个干净的页面,这些准备好了以后就可以开始了。

第一步,安装插件;

npm install cesium --save

第二步,移动文件;

在node-modules中找到刚刚安装的cesium,在文件夹中里面有一个Build文件夹,把Build里面的Cesium复制出来丢到public中

第三步,在public/index.html中,引入cesium全局样式和cesium源码,这样前期工作就已经完成了

第四步,写一个页面,并给一点样式

<template>
   <div id="my-map"></div>
</template>

<script>
export default {
  data(){
    return{
      
    }
  }
}
</script>

<style >
* {
  padding: 0;
  margin: 0;
}
#my-map {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: black;
}
</style>

第五步:需要在官网申请一个token

Cesium 官网:https://cesium.com/

步骤:

按要求填写信息

按照步骤来,创建一个token,然后最右边有token可以进行复制了

第六步:开始写代码

在刚刚的页面中写一个方法,并在mounted中使用。

 mounted() {
    this.init();
  },


 methods: {
    init() {
      this.viewer = new Cesium.Viewer("my-map", {
        homeButton: false,
        sceneModePicker: false,
        baseLayerPicker: false, // 影像切换
        animation: true, // 是否显示动画控件
        infoBox: false, // 是否显示点击要素之后显示的信息
        selectionIndicator: false, // 要素选中框
        geocoder: false, // 是否显示地名查找控件
        timeline: true, // 是否显示时间线控件
        fullscreenButton: false,
        shouldAnimate: false,
        navigationHelpButton: false, // 是否显示帮助信息控件
      });
    },
  },

但是这里会遇到一个问题,会报错

解决:在package.json文件里面的eslintConfig添加这些,然后重启项目就可以了。

得到的结果就是这样的:

第七步:优化及其他方法

  methods: {
    init() {
      (Cesium.Ion.defaultAccessToken = "你申请的cesium的token");


      this.viewer = new Cesium.Viewer("my-map", {
        homeButton: false,
        sceneModePicker: false,
        baseLayerPicker: false, // 影像切换
        animation: true, // 是否显示动画控件
        infoBox: false, // 是否显示点击要素之后显示的信息
        selectionIndicator: false, // 要素选中框
        geocoder: false, // 是否显示地名查找控件
        timeline: true, // 是否显示时间线控件
        fullscreenButton: false,
        shouldAnimate: false,
        navigationHelpButton: false, // 是否显示帮助信息控件
      });

      // 优化第一步
      //这是让你的画面以一个怎样的形式出现,相当于出场动画
      this.viewer.camera.flyTo({
        // fromDegrees()方法,将经纬度和高程转换为世界坐标,这里定位到中国
        destination: Cesium.Cartesian3.fromDegrees(101.8, 33.74, 5000000),
        orientation: {
          // 指向
          // heading:Cesium.Math.toRadians(90,0),
          // 视角
          // pitch:Cesium.Math.toRadians(-90),
          roll: 0.0,
        },
      });

      // 优化第二步
      //显示标注
      this.viewer.imageryLayers.addImageryProvider(
        new Cesium.WebMapTileServiceImageryProvider({
          url:
            "http://{s}.tianditu.gov.cn/cva_c/wmts?service=wmts&request=GetTile&version=1.0.0" +
            "&LAYER=cva&tileMatrixSet=c&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}" +
            "&style=default&format=tiles&tk=0a30a060a83ae06ba7a9dd5f70a3c203",
          layer: "tdtCva",
          style: "default",
          format: "tiles",
          tileMatrixSetID: "c",
          subdomains: ["t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7"],
          tilingScheme: new Cesium.GeographicTilingScheme(),
          tileMatrixLabels: [
            "1",
            "2",
            "3",
            "4",
            "5",
            "6",
            "7",
            "8",
            "9",
            "10",
            "11",
            "12",
            "13",
            "14",
            "15",
            "16",
            "17",
            "18",
            "19",
          ],
          maximumLevel: 18,
          show: false,
        })
      );
    },
  },

最后你就得到了一个有标注的地图

  • 16
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你解答这个问题。在vue2,你可以通过引入Cesium相关的库并在组件内部使用Cesium Cesium.Viewer 创建一个实例,然后在该实例使用Cesium的API完成点线面的绘制功能。具体实现可以参考以下代码: ```javascript <template> <div id="cesiumContainer"></div> </template> <script> import * as Cesium from 'cesium/Cesium' export default { name: 'CesiumDemo', mounted () { // 创建CesiumViewer let viewer = new Cesium.Viewer('cesiumContainer') // 设置地球初始位置及其它属性 viewer.camera.setView({ destination: Cesium.Cartesian3.fromDegrees(116.42, 39.9, 10000000) }) // 创建点元素 let point = viewer.entities.add({ name: 'Point', position: Cesium.Cartesian3.fromDegrees(116.42, 39.9), point: { color: Cesium.Color.RED, pixelSize: 10, heightReference: Cesium.HeightReference.CLAMP_TO_GROUND } }) // 创建线元素 let line = viewer.entities.add({ name: 'Line', polyline: { positions: Cesium.Cartesian3.fromDegreesArray([ 116.42, 39.9, 116.5, 40, 116.6, 39.9 ]), width: 5, material: Cesium.Color.YELLOW } }) // 创建面元素 let polygon = viewer.entities.add({ name: 'Polygon', polygon: { hierarchy: Cesium.Cartesian3.fromDegreesArray([ 116.45, 39.95, 116.45, 39.85, 116.55, 39.85, 116.55, 39.95 ]), material: Cesium.Color.BLUE.withAlpha(0.5) } }) // 将窗口设置为最大化 viewer.scene.globe.baseColor = Cesium.Color.WHITE viewer.scene.globe.enableLighting = true viewer.scene.globe.showGroundAtmosphere = true } } </script> ``` 这段代码实现了在vue2使用Cesium绘制点线面功能,具体实现步骤如下: 1. 引入Cesium相关库 2. 在组件的 `mounted` 钩子创建CesiumViewer并设置初始位置及属性 3. 使用Cesium的API创建点、线、面元素 4. 设置窗口样式 希望以上解答能够对你有所帮助,如有不清楚的地方,请不要犹豫,随时来问我!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值