希望實現:加載json外部文件的行政區划圖,實現貼地效果,並在每個行政區划的中間做一個label顯示地名,實現類似天地圖的注記功能。
首先,理解幾個知識點
#PolygonGraphics(options)感謝以前還不算差的英語,考研71分好像,至少現在讀英文API能大概知道什么意思...真的是大概
API的描述:Describes a polygon defined by an hierarchy of linear rings which make up the outer shape and any nested holes. The polygon conforms to the curvature of the globe and can be placed on the surface or at altitude and can optionally be extruded into a volume.
PolygonGraphics(多邊形面圖形)是由各個層次的線所組成的面構成(外邊、內邊),這個多邊形能夠適應球體,能夠被設置在球體表面或者特定高度,也能夠放置(縮放)在特定空間。
Name | Type | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
Object | optional Object with the following properties:
|
#PolygonGeometry
API是這么寫的A description of a polygon on the ellipsoid. The polygon is defined by a polygon hierarchy. Polygon geometry can be rendered with both Primitive
and GroundPrimitive
.
球體上的多邊形,多邊形可以是層次嵌套,多邊形幾何實例可以使用primitive方式或者GroundPrimitive
渲染,所以polygonGeometry是球體實例,只能用primitive方式渲染,使用entity方式生成實例時候就不要來查geometry的API了。
#多邊形的中點
boundingSphere類,api的定義:A bounding sphere with a center and a radius.
Cesium.BoundingSphere.fromPoints(positions, result) → BoundingSphere
#HorizontalOrigin與VerticalOrigin 平面上的水平朝向與垂直朝向
#最基礎的實現,先實現顯示注記
var jsonPromise = new Cesium.GeoJsonDataSource.load("./data/jxgeojson.json"); jsonPromise.then(function(datasource){ viewer.dataSources.add(datasource); var entities = datasource.entities.values; for (let i = 0; i < entities.length; i++) { var entity = entities[i];
//名稱用加載數據的一項屬性 entity.name = entity.properties.XZQMC;
//獲取此時此刻的點集
//整個Entity API的屬性設計是不僅僅考慮是一個常量值,而需要設置一些隨時間變換的值。
//vacr poinitsArray = entity.polygon.hierarchy.getValue(Cesium.clock.currentTime).positions;效果一樣
var pointsArray = entity.polygon.hierarchy.getValue(Cesium.JulianDate.now()).positions;
//獲取entity的polygon的中心點 var centerpoint = Cesium.BoundingSphere.fromPoints(pointsArray).center; //將entity的position設置為centerpoint,這里很好奇一個polygon沒有position么,
//如果沒有設置,確實為undefined
entity.position = centerpoint;
//設置標簽名稱 entity.label={ text:entity.name } } })
#改進顯示注記
jsonPromise.then(function(datasource){ viewer.dataSources.add(datasource); var entities = datasource.entities.values; for (let i = 0; i < entities.length; i++) { var entity = entities[i]; if(Cesium.defined(entity.polygon))//設置entity.polygon的樣式 { entity.polygon.material=new Cesium.Color.fromRandom({maximumRed:0.8,maximumBlue:0.8,maximumGreen:0.7,alpha:0.5}); //ClassificationType:Whether a classification affects terrain, 3D Tiles or both. entity.polygon.classificationType=Cesium.ClassificationType.TERRAIN; entity.name = entity.properties.XZQMC; } var pointsArray = entity.polygon.hierarchy.getValue(Cesium.JulianDate.now()).positions; var centerpoint = Cesium.BoundingSphere.fromPoints(pointsArray).center; entity.position = centerpoint; entity.label={ text:entity.name, scale:1, showBackground:true, //水平朝向,就是這個label放在position的左,中,右 horizontalOrigin:Cesium.HorizontalOrigin.LEFT, verticalOrigin:Cesium.VerticalOrigin.CENTER, distanceDisplayCondition:new Cesium.DistanceDisplayCondition(0,10000),
//遠近-大小的關系設置 scaleByDistance:new Cesium.NearFarScalar(0.5, 0.5, 3, 0.5) } } })