- 第一種,CallbackProperty 回調(diào)函數(shù)
回調(diào)函數(shù)解釋:http://www.reibang.com/p/c0bb1431f95d
label:{
//文字標簽
text: new Cesium.CallbackProperty((result) => {
let textTipsArr = textTips;
if (textTipsArr.length > 10) {
//模擬文字換行拐揭,將字符串拆成兩段
let a = textTipsArr.slice(0, 8);
let b = textTipsArr.slice(8);
result = a + "\n" + b
} else {
result = textTipsArr
}
return result;
},false),
// text: ''+textTips,
font: '500 30px Helvetica',// 15pt monospace
scale: 0.6,
style: Cesium.LabelStyle.FILL,
fillColor: Cesium.Color.WHITE,
pixelOffset: new Cesium.Cartesian2(7, 13), //偏移量
showBackground: true,
backgroundColor: new Cesium.Color.fromCssColorString('#6ab4fff2'),
maximumScale: 0.6,
minimumScale: 0.6,
disableDepthTestDistance:99000000,
heightReference:Cesium.HeightReference.CLAMP_TO_GROUND,
}
①創(chuàng)建vue組件文件 , label.vue
<template>
<div :id="id" class="divlabel-container" v-if="show" >
<div class="animate-maker-border">
<span class="animate-marker__text">{{ title }}</span>
</div>
</div>
</template>
<script>
export default {
name: "DynamicLabel",
data() {
return {
show: true,
};
},
props: {
title: {
type: String,
default: "標題",
},
id:{
type:String,
default:'001'
}
},
};
</script>
<style lang="css" scoped>
.divlabel-container{
position: absolute;
left: 0;
bottom: 0;
pointer-events: none;
cursor: pointer;
}
.divlabel-container::before {
position: absolute;
left: 0;
bottom: 0;
pointer-events: none;
cursor: pointer;
}
.divlabel-container::after {
position: absolute;
left: 0;
bottom: 0;
pointer-events: none;
cursor: pointer;
}
.animate-maker-border{
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.animate-maker-border::before{
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.animate-maker-border::after {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.animate-maker-border {
width: 150px;
height: 30px;
margin: 0;
color: #69ca62;
box-shadow: inset 0 0 0 1px rgba(105, 202, 98, 0.5);
}
.animate-maker-border::before{
content: '';
z-index: 10;
margin: -5%;
box-shadow: inset 0 0 0 2px;
animation: clipMe 8s linear infinite;
}
.animate-maker-border::after{
content: '';
z-index: 10;
margin: -5%;
box-shadow: inset 0 0 0 2px;
animation: clipMe 8s linear infinite;
}
.animate-maker-border::before {
animation-delay: -4s;
}
@keyframes clipMe {
0%, 100% {
clip: rect(0px, 170.0px, 2px, 0px);
}
25% {
clip: rect(0px, 2px, 47.0px, 0px);
}
50% {
clip: rect(45.0px, 170.0px, 47.0px, 0px);
}
75% {
clip: rect(0px, 170.0px, 47.0px, 45.0px);
}
}
.animate-marker__text {
color: #fff;
font-size: 14px;
display: flex;
width: 100%;
height: 100%;
align-items: center;
justify-content: center;
font-weight: bolder;
user-select: none;
cursor: pointer;
background: rgba(0, 173, 181, 0.32);
}
</style>
②創(chuàng)建js文件封裝類堂污,divLabel.js
/**
* @descripion:
* @param {Viewer} viewer
* @param {Cartesian2} position
* @param {String} title
* @param {String} id
* @return {*}
*/
import Vue from "vue";
import Label from "@/components/label";
let WindowVm = Vue.extend(Label);
export default class DivLabel{
constructor(val) {
this.viewer = val.viewer;
this.height = val.height;
this.position = Cesium.Cartesian3.fromDegrees(
val.position[0],
val.position[1],
val.height
);
let title = val.title;
let id = val.id
this.vmInstance = new WindowVm({
propsData: {
title,
id
}
}).$mount(); //根據(jù)模板創(chuàng)建一個面板
val.viewer.cesiumWidget.container.appendChild(this.vmInstance.$el); //將字符串模板生成的內(nèi)容添加到DOM上
this.addPostRender();
}
//添加場景事件
addPostRender() {
this.viewer.scene.postRender.addEventListener(this.postRender, this);
}
//場景渲染事件 實時更新窗口的位置 使其與笛卡爾坐標一致
postRender() {
if (!this.vmInstance.$el || !this.vmInstance.$el.style) return;
const canvasHeight = this.viewer.scene.canvas.height;
const windowPosition = new Cesium.Cartesian2();
Cesium.SceneTransforms.wgs84ToWindowCoordinates(
this.viewer.scene,
this.position,
windowPosition
);
this.vmInstance.$el.style.bottom =
canvasHeight - windowPosition.y + this.height + "px";
const elWidth = this.vmInstance.$el.offsetWidth;
this.vmInstance.$el.style.left = windowPosition.x - elWidth / 2 + "px";
const camerPosition = this.viewer.camera.position;
let height = this.viewer.scene.globe.ellipsoid.cartesianToCartographic(camerPosition).height;
height += this.viewer.scene.globe.ellipsoid.maximumRadius;
if((!(Cesium.Cartesian3.distance(camerPosition,this.position) > height))&&this.viewer.camera.positionCartographic.height<50000000){
this.vmInstance.$el.style.display = "block";
}else{
this.vmInstance.$el.style.display = "none";
}
}
}
③實際調(diào)用
import DivLabel from '@/scripts/divLabel' //根據(jù)自己的目錄引入JS類文件
let val = {
viewer:viewer,//cesium 的viewer家肯。根據(jù)實際項目填寫
position:[Number(res.data.center_point.lng), Number(res.data.center_point.lat)],//經(jīng)緯度,根據(jù)實際項目填寫
height:0,
title:res.data.center_text,
id:'210201025'+Number(res.data.center_point.lng)//ID 必須唯一
}
new DivLabel(val)