cesium 开发环境搭建( react )

一、安装 create-react-app

npm install -g create-react-app

二、react 工程创建

create-react-app cesium-react

三、cesium 安装

npm install cesium --save

四、copy-webpack-plugin 安装

npm install copy-webpack-plugin --save-dev

五、提取 webpack 配置文件

create-react-app 创建的项目,默认会隐藏 webpack 的配置项,所以需要将 webpack 配置文件提取出来。

npm run eject

成功后,项目根目录下会多出二个文件夹,config scripts,其中 webpack 的配置文件 webpack.config.js 位于 config 文件夹。

六、webpack 配置

1、添加 Cesium module name

module.exports = function (webpackEnv) {
    ...
    return {
        ...
        resolve: {
            alias: {
                // Cesium module name
                cesium: path.resolve(__dirname, '../node_modules/cesium/Source')
            }
        }
    }
}

2、添加 static files 管理

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

module.exports = function (webpackEnv) {
    ...
    return {
        ...
        resolve: {
            alias: {
                // Cesium module name
                cesium: path.resolve(__dirname, '../node_modules/cesium/Source')
            }
        },
        plugins: [
            ...
            // Copy Cesium Assets, Widgets, and Workers to a static directory
            new CopyWebpackPlugin([ { from: path.join('node_modules/cesium/Source', '../Build/Cesium/Workers'), to: 'Workers' } ]),
            new CopyWebpackPlugin([ { from: path.join('node_modules/cesium/Source', 'Assets'), to: 'Assets' } ]),
            new CopyWebpackPlugin([ { from: path.join('node_modules/cesium/Source', 'Widgets'), to: 'Widgets' } ]),
            new webpack.DefinePlugin({
                // Define relative base path in cesium for loading assets
                CESIUM_BASE_URL: JSON.stringify('')
            })
        ]
    }
}

七、Hello World

1、src/index.js 中引入样式

import 'cesium/Widgets/widgets.css'

2、src/App.js 初始化地图

import React, { Component } from 'react';
import Cesium from "cesium/Cesium";

class App extends Component {
  componentDidMount() {
    Cesium.Ion.defaultAccessToken = 'your_access_token';
    const viewer = new Cesium.Viewer("cesiumContainer");
  }
  render() {
    return (
      <div id="cesiumContainer" />
    );
  }
}

export default App;
环境如下:
node: v12.5.0
npm: 6.9.0
create-react-app: 3.0.1

github

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值