开源

CesiumJS起步

2018-08-22  本文已影响27人  布谷李

快速开始Quick start guide开始起步Getting Started

1. 确认浏览器支持Cesium

打开https://cesiumjs.org/Cesium/Apps/HelloWorld.html, 如果显示以下图片,说明浏览器支持Cesium.

Hello_world_Cesium
否则,需要做一下尝试:

2. 安装node和CesiumJS依赖

安装node开发环境,直接使用编译好的安装包安装,以node-v8.11.4版本为例

//下载安装包
$ wget https://nodejs.org/dist/v8.11.4/node-v8.11.4-linux-x64.tar.xz
//解压
$ xz -d node-v8.11.4-linux-x64.tar.xz
$ tar -xf node-v8.11.4-linux-x64.tar
//移动到/opt/目录
$ sudo mv node-v8.11.4-linux-x64 /opt/
//获取安装路径
$ pwd
/opt/node-v8.11.4-linux-x64/bin
//设置软连接
$ ln -s /opt/node-v8.11.4-linux-x64/bin/node /usr/bin/node
$ ln -s /opt/node-v8.11.4-linux-x64/bin/npm /usr/bin/npm
//测试
$ node -v
v8.11.4
$ npm -v
5.6.0
//npm配置信息可以在此指定(采用ini格式):/home/brian/.npmrc

下载CesiumJS并安装依赖包,在根目录下创建‘node_modules’ 目录.

$ wget https://github.com/AnalyticalGraphicsInc/cesium/releases/download/1.48/Cesium-1.48.zip
// 在CesiumJS根目录(package.json所在目录)运行命令,安装依赖
$ npm install
[brian@localhost Cesium-1.48]$ npm install
npm WARN deprecated gulp-util@3.0.8: gulp-util is deprecated - replace it, following the guidelines at https://medium.com/gulpjs/gulp-util-ca3b1f9f9ac5
npm WARN deprecated graceful-fs@3.0.11: please upgrade to graceful-fs 4 for compatibility with current and future versions of Node.js
npm WARN deprecated nodemailer@2.7.2: All versions below 4.0.1 of Nodemailer are deprecated. See https://nodemailer.com/status/
npm WARN deprecated minimatch@2.0.10: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
npm WARN deprecated mailcomposer@4.0.1: This project is unmaintained
npm WARN deprecated socks@1.1.9: If using 2.x branch, please upgrade to at least 2.1.6 to avoid a serious bug with socket data flow and an import issue introduced in 2.1.0
npm WARN deprecated uws@9.14.0: stop using this version
npm WARN deprecated node-uuid@1.4.8: Use uuid module instead
npm WARN deprecated buildmail@4.0.1: This project is unmaintained
npm WARN deprecated minimatch@0.2.14: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
npm WARN deprecated graceful-fs@1.2.3: please upgrade to graceful-fs 4 for compatibility with current and future versions of Node.js
npm WARN deprecated hoek@2.16.3: The major version is no longer supported. Please update to 4.x or newer
npm WARN deprecated socks@1.1.10: If using 2.x branch, please upgrade to at least 2.1.6 to avoid a serious bug with socket data flow and an import issue introduced in 2.1.0
npm WARN notice [SECURITY] timespan has the following vulnerability: 1 low. Go here for more details: https://nodesecurity.io/advisories?search=timespan&version=2.3.0 - Run `npm i npm@latest -g` to upgrade your npm version, and then `npm audit` to get more info.
npm WARN notice [SECURITY] minimatch has the following vulnerability: 1 high. Go here for more details: https://nodesecurity.io/advisories?search=minimatch&version=0.2.14 - Run `npm i npm@latest -g` to upgrade your npm version, and then `npm audit` to get more info.
npm WARN notice [SECURITY] tunnel-agent has the following vulnerability: 1 moderate. Go here for more details: https://nodesecurity.io/advisories?search=tunnel-agent&version=0.4.3 - Run `npm i npm@latest -g` to upgrade your npm version, and then `npm audit` to get more info.
npm WARN notice [SECURITY] hoek has the following vulnerability: 1 moderate. Go here for more details: https://nodesecurity.io/advisories?search=hoek&version=2.16.3 - Run `npm i npm@latest -g` to upgrade your npm version, and then `npm audit` to get more info.
npm WARN notice [SECURITY] lodash has the following vulnerability: 1 low. Go here for more details: https://nodesecurity.io/advisories?search=lodash&version=1.0.2 - Run `npm i npm@latest -g` to upgrade your npm version, and then `npm audit` to get more info.
npm WARN notice [SECURITY] minimatch has the following vulnerability: 1 high. Go here for more details: https://nodesecurity.io/advisories?search=minimatch&version=2.0.10 - Run `npm i npm@latest -g` to upgrade your npm version, and then `npm audit` to get more info.

> uws@9.14.0 install /data/work/web_wks/CesiumJS/Cesium-1.48/node_modules/uws
> node-gyp rebuild > build_log.txt 2>&1 || exit 0

npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.4 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

added 806 packages in 71.849s

3. 启动web server

官方推荐使用 Node.js ,也可以使用其他任何web server. Cesium是完全基于客户端的,不依赖服务器.

$ node server.js
Cesium development server running locally.  Connect to http://localhost:8080/

4. 启动浏览器,调用HelloWorld示例

示例地址:
http://localhost:8080/Apps/HelloWorld.html

<!DOCTYPE html>
<html lang="en">
<head>
  <!-- Use correct character set. -->
  <meta charset="utf-8">
  <!-- Tell IE to use the latest, best version. -->
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <!-- Make the application on mobile take up the full browser screen and disable user scaling. -->
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
  <title>Hello World!</title>
  <script src="../Build/Cesium/Cesium.js"></script>
  <style>
      @import url(../Build/Cesium/Widgets/widgets.css);
      html, body, #cesiumContainer {
          width: 100%; height: 100%; margin: 0; padding: 0; overflow: hidden;
      }
  </style>
</head>
<body>
  <div id="cesiumContainer"></div>
  <script>
    var viewer = new Cesium.Viewer('cesiumContainer');
  </script>
</body>
</html>

简要对该文件进行分析如下:
在脚本中引用Cesium.js

 <script src="../Build/Cesium/Cesium.js"></script>

为使用Cesium Viewer widget,需要引入它的CSS文件.

 @import url(../Build/Cesium/Widgets/widgets.css);

创建1个div
In the HTML body, we create a div for the viewer to live.

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

创建1个viewer实例

var viewer = new Cesium.Viewer('cesiumContainer');

5. 下一步

上一篇 下一篇

猜你喜欢

热点阅读