先获取多边形的 中线点

var viewer = new Cesium.Viewer("cesiumContainer");
var polygon = 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 = polygon.hierarchy.getValue(Cesium.JulianDate.now()).positions;
var polyCenter = Cesium.BoundingSphere.fromPoints(polyPositions).center;//中心点
polyCenter = Cesium.Ellipsoid.WGS84.scaleToGeodeticSurface(polyCenter);
// 添加label
redPolygon.label={
    position: polyCenter,
    text:'polygon title',
    color : Cesium.Color.fromCssColorString('#fff'),
    font:'normal 32px MicroSoft YaHei',
    showBackground : true,
    scale : 0.5,
    color: Cesium.Color.fromCssColorString('#fff'),
    font: 'normal 32px MicroSoft YaHei',
    showBackground: true,
    scale: 0.5,
    verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
    horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
    heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
};
viewer.zoomTo(viewer.entities);

 

这里的中心点是世界坐标,如果要经纬度坐标还需要转换,如下:

let cartographic = Cesium.Cartographic.fromCartesian(polyCenter, viewer.scene.globe.ellipsoid, new Cesium.Cartographic());
let lat = Cesium.Math.toDegrees(cartographic.latitude);
let lng = Cesium.Math.toDegrees(cartographic.longitude);
let height = cartographic.height;

 

posted on 2021-01-22 17:47  浅唱年华1920  阅读(4375)  评论(0编辑  收藏  举报