cesium 中的polygon本身是无法获取中心点坐标,不支持添加label,但是往往业务需求都需要添加标注,下面提供一种解决方案:
var sectorEntity = sectorCollection.add({ name: name, id:"azimuth-"+id, polygon: { hierarchy: Cesium.Cartesian3.fromDegreesArrayHeights(tirangleCoords), material: Cesium.Color.LIGHTSKYBLUE, perPositionHeight: true }, sectorId:id, station_x:sector.X, station_y:sector.Y, //stationId:baseStationId, //bsType:bsType }); var ploy = Cesium.Cartesian3.fromDegreesArrayHeights(tirangleCoords); var polyCenter = Cesium.BoundingSphere.fromPoints(ploy).center; sectorCollection.add({ position:polyCenter, label: { text: reslabel, // font: '20px Helvetica', //fillColor : Cesium.Color.SKYBLUE, //outlineColor: Cesium.Color.BLACK, //outlineWidth: 2, showBackground: false, // backgroundColor: Cesium.Color.LIME, //style: Cesium.LabelStyle.FILL_AND_OUTLINE, // outlineWidth: 1, horizontalOrigin: Cesium.HorizontalOrigin.LEFT, verticalOrigin: Cesium.VerticalOrigin.BOTTOM, heightReference:Cesium.HeightReference.RELATIVE_TO_GROUND, //pixelOffset: new Cesium.Cartesian2(0.0, -25), //pixelOffsetScaleByDistance : new Cesium.NearFarScalar(1.5e2, 3.0, 1.5e7, 0.5) scaleByDistance :new Cesium.NearFarScalar(200, 3, 900, 0.0) } });
以polygon的坐标计算中心点坐标,创建label实体