1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
| const viewer = new Cesium.Viewer("cesiumContainer", { geocoder: false, homeButton: false, sceneModePicker: false, navigationHelpButton: false, navigationInstructionsInitiallyVisible: false, animation: false, timeline: false, fullscreenButton: false, skyBox: false, shouldAnimate: true, baseLayerPicker: false, }); viewer.cesiumWidget.creditContainer.style.display = "none";
let labelListenerCallback;
function addLabel() { Sandcastle.declare(addLabel); viewer.entities.add({ position: Cesium.Cartesian3.fromDegrees(-75.1641667, 39.9522222), label: { text: "费城", }, }); }
function setFont() { Sandcastle.declare(setFont); viewer.entities.add({ position: Cesium.Cartesian3.fromDegrees(-75.1641667, 39.9522222), label: { text: "费城", font: "24px 'Maple Mono Normal NF CN'", fillColor: Cesium.Color.SKYBLUE, outlineColor: Cesium.Color.BLACK, outlineWidth: 2, style: Cesium.LabelStyle.FILL_AND_OUTLINE, }, }); }
function setProperties() { Sandcastle.declare(setProperties); const entity = viewer.entities.add({ position: Cesium.Cartesian3.fromDegrees(-75.1641667, 39.9522222, 300000.0), label: { text: "费城", }, });
entity.label.scale = 2.0; entity.label.showBackground = true; }
function offsetByDistance() { Sandcastle.declare(offsetByDistance); const image = new Image(); image.onload = function () { viewer.entities.add({ position: Cesium.Cartesian3.fromDegrees(-75.1641667, 39.9522222), billboard: { position: Cesium.Cartesian3.fromDegrees(-75.1641667, 39.9522222), scaleByDistance: new Cesium.NearFarScalar(1.5e2, 5.0, 1.5e7, 0.5), image: image, }, label: { text: "位于缩放广告牌上方的标签", font: "20px sans-serif", showBackground: true, horizontalOrigin: Cesium.HorizontalOrigin.CENTER, pixelOffset: new Cesium.Cartesian2(0.0, -image.height), 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: "纽约", 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: "亚特兰大", 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: "费城", scaleByDistance: new Cesium.NearFarScalar(1.5e2, 2.0, 1.5e7, 0.5), }, }); }
function setRightToLeft() { Sandcastle.declare(setRightToLeft); Cesium.Label.enableRightToLeftDetection = true; viewer.entities.add({ position: Cesium.Cartesian3.fromDegrees(-75.1641667, 39.9522222), label: { text: "哦读左往右从请", }, }); }
function animateLabel() { Sandcastle.declare(animateLabel); const entity = viewer.entities.add({ position: Cesium.Cartesian3.fromDegrees(-75.1641667, 39.9522222), label: { text: "费城", outlineColor: Cesium.Color.BLACK, outlineWidth: 0, style: Cesium.LabelStyle.FILL_AND_OUTLINE, }, });
let outlineDelta = 0.1; let fontDelta = -1.0; let fontSize = 20.0; const minFontSize = 1.0; const maxFontSize = 48.0;
labelListenerCallback = viewer.scene.preUpdate.addEventListener(function ( scene, time ) { entity.label.outlineWidth += outlineDelta; if (entity.label.outlineWidth >= 4.0 || entity.label.outlineWidth <= 0.0) { outlineDelta *= -1.0; }
fontSize += fontDelta; if (fontSize >= maxFontSize || fontSize <= minFontSize) { fontDelta *= -1.0; } entity.label.font = `${fontSize}px Calibri`; }); }
Sandcastle.addToolbarMenu([ { text: "添加标签", onselect: function () { addLabel(); Sandcastle.highlight(addLabel); }, }, { text: "设置字体", onselect: function () { setFont(); Sandcastle.highlight(setFont); }, }, { text: "设置属性", onselect: function () { setProperties(); Sandcastle.highlight(setProperties); }, }, { text: "按(一定)距离偏移标签", onselect: function () { offsetByDistance(); Sandcastle.highlight(offsetByDistance); }, }, { text: "按距离淡化标签", onselect: function () { fadeByDistance(); Sandcastle.highlight(fadeByDistance); }, }, { text: "按距离缩放标签", onselect: function () { scaleByDistance(); Sandcastle.highlight(scaleByDistance); }, }, { text: "设置标签,适用于右到左的语言", onselect: function () { setRightToLeft(); Sandcastle.highlight(setRightToLeft); }, }, { text: "动画标签", onselect: function () { animateLabel(); Sandcastle.highlight(animateLabel); }, }, ]);
Sandcastle.reset = function () { viewer.entities.removeAll(); if (Cesium.defined(labelListenerCallback)) { labelListenerCallback(); } };
|