本地搭建cesium环境

1、下载cesium

直接运行命令:

npm install cesium

下载完成后,找到node_modules\cesium\Build\Cesium这个文件夹,单独拎出来。
然后写一个index.html文件:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./Cesium/Widgets/widgets.css">
    <script src="./Cesium/Cesium.js"> </script>
</head>

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

</html>
<script>
    var viewer = new Cesium.Viewer('cesiumContainer');
    //加载瓦片影像
    var layer =  viewer.imageryLayers.addImageryProvider(
        new Cesium.UrlTemplateImageryProvider({ url: 'xxx' })
    );
    viewer.flyTo(layer);//视角跟随
    //加载json
    var promise = Cesium.GeoJsonDataSource.load('test.json');
    promise.then(function (dataSource) {
        console.log(dataSource);
        viewer.dataSources.add(dataSource);
        var entities = dataSource.entities.values;
        var colorHash = {};
        for (var i = 0; i < entities.length; i++) {
            var entity = entities[i];
            var name = entity.name;
            var color = colorHash[name];
            if (!color) {
//              color = Cesium.Color.fromRandom({
//                  alpha : 1.0
//              });
                color = Cesium.Color.WHITESMOKE,
                colorHash[name] = color;
            }
            entity.polygon.material = color;
            entity.polygon.outline = false;            
            entity.polygon.extrudedHeight =50.0;
            }

    });
    viewer.flyTo(promise);//视角定位

    viewer.entities.add({
        name: '小汽车',
        position: Cesium.Cartesian3.fromDegrees(110, 40, 1000),
        model: {
            uri: "./car.glb"
        }
    });
    viewer.entities.add({
        name: '文字',
        position: Cesium.Cartesian3.fromDegrees(110, 40, 1000),
        label: {
            text: 'Citizens Bank Park',
            font: '14pt monospace',
            style: Cesium.LabelStyle.FILL_AND_OUTLINE,
            outlineWidth: 2,
            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
            pixelOffset: new Cesium.Cartesian2(0, -9)
        }
    });
    viewer.entities.add({
        name: '小图片',
        position: Cesium.Cartesian3.fromDegrees(110, 40, 1000),
        billboard: {
            image: './a.png',
            width: 60,
            height: 60
        }
    });
    viewer.entities.add({
        id:'hello123',
        name: '多边形',
        polygon: {
            hierarchy: Cesium.Cartesian3.fromDegreesArray([
                -109.080842, 45.002073,
                -105.91517, 45.002073,
                -104.058488, 44.996596,
                -104.053011, 43.002989,
                -104.053011, 41.003906,
                -105.728954, 40.998429,
                -107.919731, 41.003906,
                -109.04798, 40.998429,
                -111.047063, 40.998429,
                -111.047063, 42.000709,
                -111.047063, 44.476286,
                -111.05254, 45.002073]),
            height: 0,
            material: Cesium.Color.RED.withAlpha(0.5),
            outline: true,
            outlineColor: Cesium.Color.BLACK
        }
    });
    setTimeout(()=>{
        viewer.imageryLayers.remove(layer);
        viewer.entities.removeById('hello123');//5s后移除
    },5000);

</script>
1621501272(1).png

2、搭建静态文件服务器

因为cesium在初始化的时候需要请求一些远程文件,你直接本地打开html文件是不行的,得放在一个静态文件服务器中打开,我们可以用node很方便的建一个。

安装connect 包和 serve-static 包:
npm install connect  --save
npm install serve-static  --save

写一个server.js:

var connect = require("connect");
var serveStatic = require("serve-static");
 
var app = connect();
app.use(serveStatic("./demo")); //自定义文件夹
 
app.listen(5000);

运行命令:

node server.js

这样5000端口就可以访问了,直接地址栏输入http://localhost:5000/即可访问到cesium球了。

测试资源

test.json:

{
    "type": "FeatureCollection",
    "features": [
      {
        "type": "Feature",
        "properties": {},
        "geometry": {
          "type": "Polygon",
          "coordinates": [
            [
              [
                108.8324761390686,
                34.20885647953598
              ],
              [
                108.83254051208496,
                34.207880479126565
              ],
              [
                108.83389234542847,
                34.20786273355998
              ],
              [
                108.83383870124817,
                34.208758880003636
              ],
              [
                108.8324761390686,
                34.20885647953598
              ]
            ]
          ]
        }
      },
      {
        "type": "Feature",
        "properties": {},
        "geometry": {
          "type": "Polygon",
          "coordinates": [
            [
              [
                108.83450388908386,
                34.20872338923659
              ],
              [
                108.83450388908386,
                34.20820877143483
              ],
              [
                108.83553385734558,
                34.2082974988662
              ],
              [
                108.83579134941101,
                34.2087145165425
              ],
              [
                108.83498668670654,
                34.208936333614744
              ],
              [
                108.83450388908386,
                34.20872338923659
              ]
            ]
          ]
        }
      },
      {
        "type": "Feature",
        "properties": {},
        "geometry": {
          "type": "Polygon",
          "coordinates": [
            [
              [
                108.83655309677123,
                34.2088476068559
              ],
              [
                108.83655309677123,
                34.20816440768414
              ],
              [
                108.83839845657349,
                34.20820877143483
              ],
              [
                108.83849501609802,
                34.20883873417489
              ],
              [
                108.83655309677123,
                34.2088476068559
              ]
            ]
          ]
        }
      },
      {
        "type": "Feature",
        "properties": {},
        "geometry": {
          "type": "Polygon",
          "coordinates": [
            [
              [
                108.83903145790099,
                34.208820988810025
              ],
              [
                108.83917093276978,
                34.20786273355998
              ],
              [
                108.83986830711365,
                34.20794258858026
              ],
              [
                108.83970737457275,
                34.208758880003636
              ],
              [
                108.83903145790099,
                34.208820988810025
              ]
            ]
          ]
        }
      },
      {
        "type": "Feature",
        "properties": {},
        "geometry": {
          "type": "Polygon",
          "coordinates": [
            [
              [
                108.83273363113403,
                34.207374729014184
              ],
              [
                108.83272290229797,
                34.20614139584089
              ],
              [
                108.83440732955933,
                34.20629223611846
              ],
              [
                108.8346219062805,
                34.20738360184931
              ],
              [
                108.83273363113403,
                34.207374729014184
              ]
            ]
          ]
        }
      },
      {
        "type": "Feature",
        "properties": {},
        "geometry": {
          "type": "Polygon",
          "coordinates": [
            [
              [
                108.83506178855896,
                34.20729487345586
              ],
              [
                108.83506178855896,
                34.20729487345586
              ],
              [
                108.83506178855896,
                34.20729487345586
              ],
              [
                108.83506178855896,
                34.20729487345586
              ]
            ]
          ]
        }
      },
      {
        "type": "Feature",
        "properties": {},
        "geometry": {
          "type": "Polygon",
          "coordinates": [
            [
              [
                108.8350510597229,
                34.2072682549196
              ],
              [
                108.83504033088684,
                34.20665602626555
              ],
              [
                108.83703589439392,
                34.20660278878113
              ],
              [
                108.83745431900024,
                34.20741022034913
              ],
              [
                108.8350510597229,
                34.2072682549196
              ]
            ]
          ]
        }
      },
      {
        "type": "Feature",
        "properties": {},
        "geometry": {
          "type": "Polygon",
          "coordinates": [
            [
              [
                108.83790493011473,
                34.20745458449684
              ],
              [
                108.83786201477051,
                34.20647856785336
              ],
              [
                108.83999705314636,
                34.20656729710617
              ],
              [
                108.84015798568726,
                34.20754331272217
              ],
              [
                108.83790493011473,
                34.20745458449684
              ]
            ]
          ]
        }
      },
      {
        "type": "Feature",
        "properties": {},
        "geometry": {
          "type": "Polygon",
          "coordinates": [
            [
              [
                108.83275508880615,
                34.20536057126524
              ],
              [
                108.83278727531432,
                34.203967491274426
              ],
              [
                108.83497595787048,
                34.20414495497323
              ],
              [
                108.83516907691956,
                34.205271840742135
              ],
              [
                108.83275508880615,
                34.20536057126524
              ]
            ]
          ]
        }
      },
      {
        "type": "Feature",
        "properties": {},
        "geometry": {
          "type": "Polygon",
          "coordinates": [
            [
              [
                108.83585572242737,
                34.20503226786322
              ],
              [
                108.83583426475525,
                34.20434903776502
              ],
              [
                108.8400936126709,
                34.204641851342345
              ],
              [
                108.8401472568512,
                34.20556465111387
              ],
              [
                108.83585572242737,
                34.20503226786322
              ]
            ]
          ]
        }
      }
    ]
  }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,324评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,356评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,328评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,147评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,160评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,115评论 1 296
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,025评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,867评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,307评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,528评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,688评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,409评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,001评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,657评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,811评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,685评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,573评论 2 353

推荐阅读更多精彩内容