cesium 1.52 demo _ Labels.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
    <meta name="description" content="Create and style textual labels.">
    <meta name="cesium-sandcastle-labels" content="Showcases">
    <title>Labels</title>
    <script type="text/javascript" src="../Sandcastle-header.js"></script>
    <script type="text/javascript" src="../../../ThirdParty/requirejs-2.1.20/require.js"></script>
    <script type="text/javascript">
    require.config({
        baseUrl : '../../../Source',
        waitSeconds : 60
    });
    </script>
</head>
<body class="sandcastle-loading" data-sandcastle-bucket="bucket-requirejs.html">
    <style>
        @import url(../templates/bucket.css);
    </style>
    <div id="cesiumContainer" class="fullSize"></div>
    <div id="loadingOverlay"><h1>Loading...</h1></div>
    <div id="toolbar"></div>
    <script id="cesium_sandcastle_script">

        function startup(Cesium){
            'use strict';
            // Sandcastle_Begin
            let viewer = new Cesium.Viewer('cesiumContainer');

            function addLabel() {
                Sandcastle.declare(addLabel);
                viewer.entities.add({
                    position : Cesium.Cartesian3.fromDegrees(-75.1641667, 39.9522222),
                    label : {
                        text : 'Philadelphia'
                    }
                });
            }

            function setFont() {
                Sandcastle.declare(setFont);
                viewer.entities.add({
                    position : Cesium.Cartesian3.fromDegrees(-75.1641667, 39.9522222),
                    label : {
                        text : 'Philadelphia',  // 文本。
                        font : '24px Helvetica',
                        fillColor : Cesium.Color.SKYBLUE,  // 填充颜色。
                        outlineColor : Cesium.Color.BLACK,  // 轮廓颜色。
                        outlineWidth : 2,  // 轮廓宽度。
                        style : Cesium.LabelStyle.FILL_AND_OUTLINE  // 样式。
                    }
                });
            }

            function setProperties() {
                Sandcastle.declare(setProperties);
                let entity = viewer.entities.add({
                    position : Cesium.Cartesian3.fromDegrees(-75.1641667, 39.9522222, 300000.0),
                    label : {
                        text : 'Philadelphia'
                    }
                });

                entity.label.scale = 2.0;  // 放大到原来的2倍。
                entity.label.showBackground = true;  // 展示背景。
            }

            function offsetByDistance() {
                Sandcastle.declare(offsetByDistance);
                let image = new Image();
                image.onload = function() {
                    viewer.entities.add({
                        position : Cesium.Cartesian3.fromDegrees(-75.1641667, 39.9522222),
                        billboard : {
                            // 按距离缩放billboard。
                            // billboard距离相机1.5e2米时,其大小是原来的5倍;1.5e7米时, 是0.5倍。
                            scaleByDistance : new Cesium.NearFarScalar(1.5e2, 5.0, 1.5e7, 0.5),
                            image : image
                        },
                        label : {
                            text : 'Label on top of scaling billboard',
                            font : '20px sans-serif',
                            showBackground : true,
                            horizontalOrigin : Cesium.HorizontalOrigin.CENTER,
                            pixelOffset : new Cesium.Cartesian2(0.0, -image.height),
                            // 根据距相机的距离设置像素偏移量。(水平)
                            // label距离相机1.5e2米时,其沿x正向偏移3.0px; 1.5e7米时,正向偏移0.5。
                            pixelOffsetScaleByDistance : new Cesium.NearFarScalar(1.5e2, 3.0, 1.5e7, 0.5)
                        }
                    });
                };
                image.src = '../images/facility.gif';
            }

            function fadeByDistance() {
                Sandcastle.declare(fadeByDistance);
                viewer.entities.add({
                    position : Cesium.Cartesian3.fromDegrees(-73.94, 40.67),
                    label : {
                        text : 'New York',
                        // 根据距离相机的距离设置半透明。
                        // label距离相机1.5e2米时,不透明;1.5e8米时,透明。
                        translucencyByDistance : new Cesium.NearFarScalar(1.5e2, 1.0, 1.5e8, 0.0)
                    }
                });
                viewer.entities.add({
                    position : Cesium.Cartesian3.fromDegrees(-84.39, 33.75),
                    label : {
                        text : 'Atlanta',
                        translucencyByDistance : new Cesium.NearFarScalar(1.5e5, 1.0, 1.5e7, 0.0)
                    }
                });
            }

            function scaleByDistance() {
                Sandcastle.declare(scaleByDistance);

                viewer.entities.add({
                    position : Cesium.Cartesian3.fromDegrees(-75.1641667, 39.9522222),
                    label : {
                        text : 'Philadelphia',
                        // 根据距摄像机的距离设定缩放。
                        // new Cesium.NearFarScalar(near, nearValue, far, farValue)
                        scaleByDistance : new Cesium.NearFarScalar(1.5e2, 2.0, 1.5e7, 0.5)
                    }
                });
            }

            function setRightToLeft() {
                Sandcastle.declare(setRightToLeft);
                // 启用从右到左的检测。
                //Only needs to be set once at the beginning of the application.
                Cesium.Label.enableRightToLeftDetection = true;
                viewer.entities.add({
                    position : Cesium.Cartesian3.fromDegrees(-75.1641667, 39.9522222),
                    label : {
                        text : 'Master (אדון): Hello\nתלמיד (student): שלום'
                    }
                });
            }

            Sandcastle.addToolbarMenu([
                {
                    text : 'Add label',
                    onselect : function() {
                        addLabel();
                        // 此行代码,经测试没有作用。
                        // Sandcastle.highlight(addLabel);
                    }
                },
                {
                    text : 'Set font',
                    onselect : function() {
                        setFont();
                        // Sandcastle.highlight(setFont);
                    }
                },
                {
                    text : 'Set properties',
                    onselect : function() {
                        setProperties();
                        // Sandcastle.highlight(setProperties);
                    }
                },
                {
                    text : 'Offset label by distance',
                    onselect : function() {
                        offsetByDistance();
                        // Sandcastle.highlight(offsetByDistance);
                    }
                },
                {
                    text : 'Fade label by distance',
                    onselect : function() {
                        fadeByDistance();
                        // Sandcastle.highlight(fadeByDistance);
                    }
                },
                {
                    text : 'Scale label by distance',
                    onselect : function() {
                        scaleByDistance();
                        // Sandcastle.highlight(scaleByDistance);
                    }
                },
                {
                    text : 'Set label with right-to-left language',
                    onselect : function() {
                        setRightToLeft();
                        // Sandcastle.highlight(setRightToLeft);
                    }
                }
            ]);

            Sandcastle.reset = function() {
                // 每次切换标签,移除所有实体。
                viewer.entities.removeAll();
            };
            //Sandcastle_End
            Sandcastle.finishedLoading();
        }

        if (typeof Cesium !== 'undefined') {
            startup(Cesium);
        }
        else if (typeof require === 'function') {
            require(['Cesium'], startup);
        }
    </script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_42193179/article/details/85782764