一、學習資料:http://cesiumjs.org/tutorials.html,看完6個教程后對圖層加載、控件控制開關、地形數據疊加、模型添加、相機控制、圖形繪制有一點了解。這也是cesium的主要功能。
二、下載cesium 1.3的壓縮包。
1.模塊功能的演示:Apps/Sandcastle/gallery,能更加快速的入門。
2.API查詢。Build\Documentation\,對函數、參數有了解。
三、實踐:主要實現車輛位置的監控及廠區、道路的顯示。
1.初始化控件
- <span style="white-space:pre"> </span>var viewer = new Cesium.Viewer('cesiumContainer', {
- //baseLayerPicker : false,//圖層控制顯示
- animation: false, //動畫控制不顯示
- timeline: false,//時間線不顯示
- sceneModePicker: false,//投影方式顯示
- navigationHelpButton: false//幫助不顯示
- });
2.加載geojson道路和廠區建築
- <span style="white-space:pre"> </span> var billboards = new Cesium.BillboardCollection();//圖形集合
- scene.primitives.add(billboards);
- var labels = new Cesium.LabelCollection();//文字集合
- scene.primitives.add(labels);
- <span style="white-space:pre"> </span> //加載geojson
- function loadGeoJson(url) {
- var dtd = $.Deferred();//這個Deferred可以看看jquery的資料
- var dataSource = new Cesium.GeoJsonDataSource();
- viewer.dataSources.add(dataSource);
- dataSource.loadUrl(url).then(function () {
- var entities = dataSource.entities.entities;
- var colorHash = {};
- for (var i = 0; i < entities.length; i++) {
- var entity = entities[i];
- var colorstr = entity.properties["color"];
- var ispolygon = entity.polygon;
- var ispolyline = entity.polyline;
- var color = Cesium.Color.BLUE;
- if (colorstr) {
- color = colorHash[colorstr];
- if (!color) {
- color = Cesium.Color.fromRandom({
- alpha: 0.6
- });//這里采用隨機,api中顏色用的是rgba的32位
- colorHash[name] = color;
- }
- }
- if (ispolygon)//面處理
- {
- entity.polygon.material = Cesium.ColorMaterialProperty.fromColor(color);
- var height=entity.properties["height"];//要素的屬性字段
- var x = entity.properties["x"];
- var y = entity.properties["y"];
- var name = entity.properties["NAME"];
- if (height) {
- entity.polygon.extrudedHeight = new Cesium.ConstantProperty(height);//拔高
- <span style="white-space:pre"> </span>//深入可以考慮材質貼文理
- }
- if (x && y)
- {
- if (!height)
- { height = 0; }
- if (name) {
- buildlabels.add({//添加面的標注
- position: Cesium.Cartesian3.fromDegrees(x, y, height+5),
- text: name,
- scale: 0.8,
- translucencyByDistance: new Cesium.NearFarScalar(2000, 1, 3000, 0),//代表2000米,透明度1。3000米,文字透明度0
- fillColor: new Cesium.Color(0.6, 0.9, 1.0),
- outlineColor: Cesium.Color.BLACK,
- outlineWidth: 2,
- style: Cesium.LabelStyle.FILL_AND_OUTLINE
- });
- }
- }
- }
- else if (ispolyline)//線處理
- {
- entity.polyline.material = Cesium.ColorMaterialProperty.fromColor(color);
- }
- }
- dtd.resolve();
- });
- return dtd;
- }

- <span style="font-size:18px;"><span style="white-space:pre"> </span>$.when(loadGeoJson('data/road5.geojson'), loadGeoJson('data/build6.geojson'), zoomToCenter(x, y))
- .done(function () {
- setInterval(refreshCar, 10000);
- jBox2.tip("加載完成", 'success');
- })
- .fail(function () {
- jBox2.tip("加載失敗", 'error');
- });</span></span>

- <span style="white-space:pre"> </span>function createModel2(car) {
- var x = car.jin;
- var y = car.wei;
- var carid = car.carid;
- labels.add({//給車子添加標注
- position: Cesium.Cartesian3.fromDegrees(x, y, 5),
- text: carid,
- scale: 0.8,
- translucencyByDistance: new Cesium.NearFarScalar(200, 1, 500, 0)
- });
- billboards.add({
- image: 'image/car.gif',
- position: Cesium.Cartesian3.fromDegrees(x, y, 0),
- scaleByDistance: new Cesium.NearFarScalar(0, 2, 10000, 0)//根據距離放大縮小圖片
- });
- }
- <span style="white-space:pre"> </span> function createModel(url, x, y, height, rotate) {
- height = Cesium.defaultValue(height, 0.0);
- var ellipsoid = viewer.scene.globe.ellipsoid;
- var cart3 = ellipsoid.cartographicToCartesian(Cesium.Cartographic.fromDegrees(x, y, height));
- var modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(cart3);
- var quat = Cesium.Quaternion.fromAxisAngle(Cesium.Cartesian3.UNIT_Z, Cesium.Math.toRadians(rotate));
- var mat3 = Cesium.Matrix3.fromQuaternion(quat);
- var mat4 = Cesium.Matrix4.fromRotationTranslation(mat3, Cesium.Cartesian3.ZERO);
- var m = Cesium.Matrix4.multiplyTransformation(modelMatrix, mat4, new Cesium.Matrix4());
- var model = scene.primitives.add(Cesium.Model.fromGltf({
- url: url,//模型地址
- modelMatrix: m,//模型轉換矩陣,像角度,之類
- minimumPixelSize: 30,//地圖上顯示最小像素
- scale: 10//模型放大比例
- }));
- // model.readyToRender.addEventListener(function (model) {//如果模型有動畫,開啟動畫
- //Play and loop all animations at half-speed
- // model.activeAnimations.addAll({
- // speedup : 0.5,
- // loop : Cesium.ModelAnimationLoop.REPEAT
- // });
- //
- // });
- };
6,模型處理過程
sketchup下載模型-》導出dae格式-》將圖片及dae壓縮成zip格式-》 http://cesiumjs.org/convertmodel.html-》轉換完成自動下載glTF文件-》copy glTF文件及圖片文件到工程目錄下。當中碰見過問題是圖片格式是bmp,但是glTF中寫的是png,可以手動修改下文件中相應圖片的后綴。
四、綜合
1.cesium基於webgl,實踐中性能不佳,特別是模型這塊支持不理想。對矢量操作確實不錯,商用有差距。
2.官網有些插件可以下載直接使用,如cesium-drawhelper(畫圖工具)
3.可以參考下這位的博客:http://my.oschina.net/u/1585572/blog/287961