vue2安装cesium并使用

一、安装

1.安装cesium

javascript 复制代码
npm install cesium@1.95.0 -S

2.安装所需

javascript 复制代码
npm install copy-webpack-plugin@10.2.4 -D

二、配置

1.配置vue.config.js

vue 中引入cesium 需要用copy-webpack-plugin 把一些文件拷贝到打包目录

javascript 复制代码
// vue.config.js
const CopyWebpackPlugin = require("copy-webpack-plugin");
const webpack = require("webpack");


const path = require("path"); //引入path模块
function resolve(dir) {
  return path.join(__dirname, dir); //path.join(__dirname)设置绝对路径
}
module.exports = {
  lintOnSave: false,
  // publicPath: '/xayw_jc',
  productionSourceMap: false, //关闭 SourceMap,webpack://
  // publicPath:
  //   process.env.NODE_ENV === "production" ? "/\n" + "vue-cesium-example/" : "/",
  configureWebpack: {
    resolve: {
      alias: {
        "@": resolve("src"),
      },
    },
    plugins: [
      // Copy Cesium Assets, Widgets, and Workers to a static directory
      new CopyWebpackPlugin({
        patterns: [{
          from: "node_modules/cesium/Build/Cesium/Workers",
          to: "Workers"
        },
        {
          from: "node_modules/cesium/Build/Cesium/ThirdParty",
          to: "ThirdParty",
        },
        {
          from: "node_modules/cesium/Build/Cesium/Assets",
          to: "Assets"
        },
        {
          from: "node_modules/cesium/Build/Cesium/Widgets",
          to: "Widgets"
        },
        ],
      }),
      new webpack.DefinePlugin({
        // Define relative base path in cesium for loading assets
        CESIUM_BASE_URL: JSON.stringify(""),
      }),

      // new BundleAnalyzerPlugin() //解开可查看项目打包分布情况
    ],
    module: {
      // Removes these errors: "Critical dependency: require function is used in a way in which dependencies cannot be statically extracted"
      // https://github.com/AnalyticalGraphicsInc/cesium-webpack-example/issues/6
      unknownContextCritical: false,
      unknownContextRegExp: /\/cesium\/cesium\/Source\/Core\/buildModuleUrl\.js/,
      rules: [
        {
          test: /\.mjs$/,
          include: /node_modules/,
          type: "javascript/auto"
        },
      ]
    },
  },
  chainWebpack: (config) => {
    config.module.rule("svg").exclude.add(resolve("src/icons")).end();
    config.module
      .rule("icons")
      .test(/\.svg$/)
      .include.add(resolve("src/icons"))
      .end()
      .use("svg-sprite-loader")
      .loader("svg-sprite-loader")
      .options({
        symbolId: "icon-[name]",
      })
      .end();
  },
};

2.配置css

①高版本的cesium引入widgets.css会报错,需要到node_modules/cesium/package.json添加

javascript 复制代码
 "./widgets.css":"./Source/Widgets/widgets.css",

②main.js引入

javascript 复制代码
import 'cesium/widgets.css';

3.获取Access Tokens

cesium 网站获取token,并复制到项目中

三、开发

cesium页面代码如下

javascript 复制代码
<template>
  <div id="cesiumContainer">
  </div>
</template>
<script>
import * as Cesium from 'cesium'
export default {
  name: 'HelloWorld',
  mounted(){
  this.initCesium()
  },
  methods:{
    initCesium (){
      Cesium.Ion.defaultAccessToken = "网站复制的token"
      let viewer = new Cesium.Viewer('cesiumContainer')
    }
  }
}
</script>
<style scoped>
 
</style>

运行效果如下

相关推荐
汪洪墩16 小时前
【Mars3d】设置backgroundImage、map.scene.skyBox、backgroundImage来回切换
开发语言·javascript·python·ecmascript·webgl·cesium
supermapsupport21 小时前
iClent3D for Cesium 实现无人机巡检飞行效果
gis·cesium·supermap·webgis
不浪brown1 天前
仅需3行代码!带你做一个3D卫星轨道飞行动画
cesium
白嫖叫上我3 天前
Cesium 无人机航线规划(航点航线)
无人机·cesium
BJ-Giser5 天前
前端解析超图的iserver xml
前端·可视化·cesium
我码玄黄5 天前
在Cesium中加载OD线
前端·javascript·cesium
supermapsupport5 天前
iClient3D for Cesium 实现限高分析
3d·vue·gis·cesium·supermap·webgis
白嫖叫上我6 天前
Cesium 无人机航线规划(区域航线)
无人机·cesium
智方科技10 天前
探索 Cesium 的未来:3D Tiles Next 标准解析
前端·算法·3d·信息可视化·webgl·cesium·tilesbuilder
Allen.l12 天前
Cesium中实现仿ArcGIS三维的动态图层加载方式
javascript·arcgis·cesium