Cesium 初始化地球

Cesium 初始化地球

1.项目/环境搭建

新建一个React项目,然后npm安装一下Cesium

npm install cesium
// 或者
cnpm install cesium
// 或者
yarn add cesium

另外,react项目中集成Cesium需要配置webpack,因此需要安装copy-webpack-plugin插件并把webpack给暴露出来。

安装copy-webpack-plugin,注意插件的版本不要高于7!!!

npm install copy-webpack-plugin@6.4.1
// 或者
cnpm install copy-webpack-plugin@6.4.1

运行命令:npm run eject或者yarn eject将webpack配置给暴露出来,执行完之后你就会发现项目中多了几个文件夹,接下去就是要修改webpack配置文件,打开config文件夹下的webpack.config.js文件:

// 先一模一样照着写就行,申明资源路径
// Cesium源码所在目录
const CopyWebpackPlugin = require('copy-webpack-plugin');
const cesiumSource = 'node_modules/cesium/Source';
const cesiumWorkers = '../Build/Cesium/Workers';
const fileFolder = 'src';

在这里插入图片描述
在return中的output对象中加入sourcePrefix: ‘’,让webpack字符串缩进正确
在这里插入图片描述
output下面添加同级amd对象

amd: {
    // Enable webpack-friendly use of require in Cesium
    toUrlUndefined: true,
},

在这里插入图片描述
修改resolve中的alias

alias: {
  // Support React Native Web
  // https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
  'react-native': 'react-native-web',
   cesium: path.resolve(cesiumSource),  // 新增这一行数据即可
},

在这里插入图片描述
在plugins中加入打包时的配置(注意,这里的plugins不是resolve下的plugins,而是与它同级的plugins。):

new CopyWebpackPlugin(
{
    patterns: [
    	{ from: path.join(cesiumSource, cesiumWorkers), to: 'Workers' },
    ]
}),
new CopyWebpackPlugin(
{
    patterns: [
    	{ from: path.join(cesiumSource, 'Assets'), to: 'Assets' },
    ]
}
),
new CopyWebpackPlugin(
{
    patterns: [
    	{ from: path.join(cesiumSource, 'Widgets'), to: 'Widgets' },
    ]
}
),
// new CopyWebpackPlugin(
//   {
//     patterns: [
//       { from: path.join(fileFolder, 'data'), to: 'Data' }
//     ]
//   }
// ),//data项是用来存放本地数据的,当需要读取本地文件时(图片、模型)需要在src文件夹下新建data文件夹存放数据,没有data可以先注释
// Makes some environment variables available to the JS code, for example:
// if (process.env.NODE_ENV === 'production') { ... }. See `./env.js`.
// It is absolutely essential that NODE_ENV is set to production
// during a production build.
// Otherwise React will be compiled in the very slow development mode.
new webpack.DefinePlugin({
	// env.stringified,
	CESIUM_BASE_URL: JSON.stringify(''),
}),

配置loader

由于在 ./node_modules/cesium/Source/ThirdParty/zip.js文件中使用了import.meta语法,webpack 默认不支持,在进行项目构建时,会报如下错误,提示信息需要添加 loader。执行下面的命令:

npm install @open-wc/webpack-import-meta-loader --save-dev
// 或者
cnpm install @open-wc/webpack-import-meta-loader --save-dev

然后在webpack.config.js中添加:

module: {
 rules: [
     {
         test: /\.js$/,
         use: {
         	loader: '@open-wc/webpack-import-meta-loader',
         },
     },
 ]}

至此为止,项目的环境以及基本搭建完毕。

2.初始化代码

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import 'cesium/Widgets/widgets.css'

ReactDOM.render(
    <App />,
  document.getElementById('root')
);

App.js

import React, { Component } from 'react';
import { Viewer } from "cesium/Cesium";
export default class App extends Component {
  componentDidMount() {
    //地图初始化
    const viewer = new Viewer("cesiumContainer");
  }
  render() {
    return (
      <div id="cesiumContainer" />
    );
  }
}

App.css

#cesiumContainer {
  height: 100vh;
}

完事之后npm start一下或者yarn start一下:就能看到经典地球了:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

偶尔躲躲乌云_0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值