Cesium绘制polygon并添加label的方法
方法1:
var viewer = new Cesium.Viewer("cesiumContainer");
var redPolygon = viewer.entities.add({
name: "Red polygon on surface",
polygon: {
hierarchy: Cesium.Cartesian3.fromDegreesArray([-115.0,37.0,-115.0,32.0,-107.0,33.0,-102.0,31.0,-102.0,35.0, ]),
material: Cesium.Color.RED.withAlpha(0.5),
},
});
var polyPositions = redPolygon.polygon.hierarchy.getValue(Cesium.JulianDate.now()).positions;
var polyCenter = Cesium.BoundingSphere.fromPoints(polyPositions).center;//中心点
polyCenter = Cesium.Ellipsoid.WGS84.scaleToGeodeticSurface(polyCenter);
redPolygon.position = polyCenter;
redPolygon.label={
// position:
text:'Red polygon on surface',
color : Cesium.Color.fromCssColorString('#fff'),
font:'normal 32px MicroSoft YaHei',
showBackground : true,
scale : 0.5,
horizontalOrigin : Cesium.HorizontalOrigin.LEFT_CLICK,
verticalOrigin : Cesium.VerticalOrigin.BOTTOM,
disableDepthTestDistance : 10000.0
};
viewer.zoomTo(viewer.entities);
方法2:
var viewer = new Cesium.Viewer("cesiumContainer");
var redPolygon = viewer.entities.add({
name: "Red polygon on surface",
polygon: {
hierarchy: Cesium.Cartesian3.fromDegreesArray([-115.0,37.0,-115.0,32.0,-107.0,33.0,-102.0,31.0,-102.0,35.0, ]),
material: Cesium.Color.RED.withAlpha(0.5),
},
});
redPolygon.position=Cesium.Cartesian3.fromDegrees(-115.0,37.0);
redPolygon.label={
// position:
text:'Red polygon on surface',
color : Cesium.Color.fromCssColorString('#fff'),
font:'normal 32px MicroSoft YaHei',
showBackground : true,
scale : 0.5,
horizontalOrigin : Cesium.HorizontalOrigin.LEFT_CLICK,
verticalOrigin : Cesium.VerticalOrigin.BOTTOM,
disableDepthTestDistance : 10000.0
};
viewer.zoomTo(viewer.entities);
效果图3: