cesium自定義label標簽溉愁,cesium 文字標簽換行处铛,cesium label標簽識別html

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)


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末盟猖,一起剝皮案震驚了整個濱河市讨衣,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌扒披,老刑警劉巖值依,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異碟案,居然都是意外死亡愿险,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進店門价说,熙熙樓的掌柜王于貴愁眉苦臉地迎上來辆亏,“玉大人,你說我怎么就攤上這事鳖目“邕叮” “怎么了?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵领迈,是天一觀的道長彻磁。 經(jīng)常有香客問我,道長狸捅,這世上最難降的妖魔是什么衷蜓? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮尘喝,結(jié)果婚禮上磁浇,老公的妹妹穿的比我還像新娘。我一直安慰自己朽褪,他們只是感情好置吓,可當我...
    茶點故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布无虚。 她就那樣靜靜地躺著,像睡著了一般衍锚。 火紅的嫁衣襯著肌膚如雪友题。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天构拳,我揣著相機與錄音咆爽,去河邊找鬼。 笑死置森,一個胖子當著我的面吹牛斗埂,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播凫海,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼呛凶,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了行贪?” 一聲冷哼從身側(cè)響起漾稀,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎建瘫,沒想到半個月后崭捍,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡啰脚,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年殷蛇,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片橄浓。...
    茶點故事閱讀 39,992評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡粒梦,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出荸实,到底是詐尸還是另有隱情匀们,我是刑警寧澤,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布准给,位于F島的核電站泄朴,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏露氮。R本人自食惡果不足惜叼旋,卻給世界環(huán)境...
    茶點故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望沦辙。 院中可真熱鬧,春花似錦讹剔、人聲如沸油讯。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽陌兑。三九已至沈跨,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間兔综,已是汗流浹背饿凛。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留软驰,地道東北人涧窒。 一個月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像锭亏,于是被迫代替她去往敵國和親纠吴。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,947評論 2 355

推薦閱讀更多精彩內(nèi)容