Vue + Cesium configuration

Version, I use cli4.5 and Cesium1.74

1. Create a vue project

vue create 项目名

2. Install Cesium

Enter the project directory, enter the following code

npm install cesium

3. Create the vue.config.js file

Create a vue.config.js file in the project root directory, and copy the following code into the file

const CopyWebpackPlugin = require('copy-webpack-plugin')

module.exports = {
    
    
  configureWebpack: {
    
    
    plugins: [
      new CopyWebpackPlugin([{
    
     from: 'node_modules/cesium/Build/Cesium/Workers', to: 'Workers' }]),
      new CopyWebpackPlugin([{
    
     from: 'node_modules/cesium/Build/Cesium/ThirdParty', to: 'ThirdParty' }]),
      new CopyWebpackPlugin([{
    
     from: 'node_modules/cesium/Build/Cesium/Assets', to: 'Assets' }]),
      new CopyWebpackPlugin([{
    
     from: 'node_modules/cesium/Build/Cesium/Widgets', to: 'Widgets' }])
    ]
  }
}
  1. Create CesiumDemo.vue file

Note: You need to register an account at https://cesium.com/ion/tokens in advance, then obtain the token, and then modify the token in Cesium.Ion.defaultAccessToken.

Create the CesiumDemo.vue file in the components directory and enter the following code

<template>
  <div id="cesiumContainer"></div>
</template>

<script>
window.CESIUM_BASE_URL = '/'
import * as Cesium from 'cesium'
import 'cesium/Build/Cesium/Widgets/widgets.css'
export default {
    
    
  name: 'CesiumDemo',
  mounted() {
    
    
    // Your access token can be found at: https://cesium.com/ion/tokens.
    Cesium.Ion.defaultAccessToken = '自己的token'
    // Initialize the Cesium Viewer in the HTML element with the "cesiumContainer" ID.
    const viewer = new Cesium.Viewer('cesiumContainer', {
    
    
      terrainProvider: Cesium.createWorldTerrain()
    })
    // Add Cesium OSM Buildings, a global 3D buildings layer.
    // const buildingTileset = viewer.scene.primitives.add(Cesium.createOsmBuildings())
    // Fly the camera to San Francisco at the given longitude, latitude, and height.
    viewer.camera.flyTo({
    
    
      destination: Cesium.Cartesian3.fromDegrees(-122.4175, 37.655, 400),
      orientation: {
    
    
        heading: Cesium.Math.toRadians(0.0),
        pitch: Cesium.Math.toRadians(-15.0)
      }
    })
  }
}
</script>

<style></style>

  1. Run the project
    npm run serve

Guess you like

Origin blog.csdn.net/qq_17627195/article/details/109199968