自定義繪制多邊形

剛剛分享了地形開挖膜楷,覺得可能大部分業(yè)務(wù)是自定義多邊形砾嫉,然后開挖抡砂,基于此大咱,分享自定義繪制多邊形函數(shù),引入即可使用

// 構(gòu)造唯一id
import { v4 as uuidv4 } from 'uuid';
// 坐標(biāo)轉(zhuǎn)化
const getLonLatFromCartesian = (cartesian3) => {
    const { longitude, latitude, height } = Cesium.Cartographic.fromCartesian(cartesian3);
    return {
        lng: +Cesium.Math.toDegrees(longitude),
        lat: +Cesium.Math.toDegrees(latitude),
        height: +height
    }
};
// 創(chuàng)建點(diǎn)
const createPoint = (viewer, Cartesian, options) => {
    const { lng, lat, height } = getLonLatFromCartesian(Cartesian)
    return viewer.entities.add({
        position: new Cesium.Cartesian3.fromDegrees(lng, lat, height),
        point: {
            pixelSize: options.pixelSize, 
            color: options.pointColor   
        }
    })
};
// 繪制多邊形
const DrawPolygon = (viewer, options = {}, callback) => {
    if (!viewer) throw new Error("no viewer object!");
    options = {
      id: uuidv4(),
      pointColor: Cesium.Color.fromCssColorString('#FFC107'),
      pixelSize: 8,
      outlineWidth: 5,
      outlineColor: new Cesium.Color(0, 64 / 255, 133 / 255, 0.8),
      color: Cesium.Color.fromCssColorString('rgba(0, 123, 255, 0.4)'),
      ...options
    };
    if (viewer.entities.getById(options.id)) {
      throw new Error("the id is an unique value");
    }
    const handler = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
    let activeShapePoints = [];
    let polygon = undefined;
    let floatingPoint = undefined;
    // 鼠標(biāo)左擊事件
    handler.setInputAction((event) => {
      let ray = viewer.camera.getPickRay(event.position);
      let earthPosition = viewer.scene.globe.pick(ray, viewer.scene);
      // 鼠標(biāo)開始點(diǎn)擊多邊形的第一個(gè)點(diǎn)
      if (activeShapePoints.length === 0) {
        floatingPoint = createPoint(viewer, earthPosition, options);
        // 將第一個(gè)左擊的點(diǎn)添加到多邊形頂點(diǎn)集合中
        activeShapePoints.push(earthPosition);
        // 多邊形的坐標(biāo)采用回調(diào)的形式
        // 多邊形邊界頂點(diǎn)
        let polylinePoints = new Cesium.CallbackProperty(() => {
          let vertexPoints = activeShapePoints.concat([activeShapePoints[0]]);
          return vertexPoints;
        }, false);
        // 多邊形頂點(diǎn)
        let dynamicPositions = new Cesium.CallbackProperty(() => {
          return new Cesium.PolygonHierarchy(activeShapePoints);
        }, false);
        // 添加一個(gè)多邊形
        polygon = viewer.entities.add({
          name: "customPolygon",
          id: options.id,
          polyline: {
            positions: polylinePoints,
            width: options.outlineWidth,
            material: options.outlineColor,
            clampToGround: true,
          },
          polygon: {
            hierarchy: dynamicPositions,
            material: options.color,
            heightReference: Cesium.HeightReference.NONE,
          },
        });
      }
      // 將鼠標(biāo)點(diǎn)擊的點(diǎn)添加到多邊形頂點(diǎn)集合中
      activeShapePoints.push(earthPosition);
      // 記錄多邊形邊界的點(diǎn)
      polygon.outLinePoint = (polygon.outLinePoint ?? []).concat(createPoint(viewer, earthPosition, options));
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
    // 鼠標(biāo)移動(dòng)事件
    handler.setInputAction((movement) => {
      // 獲取鼠標(biāo)所在的位置
      const ray = viewer.camera.getPickRay(movement.endPosition);
      const newPosition = viewer.scene.globe.pick(ray, viewer.scene);
      // 已經(jīng)定義了polygon
      if (Cesium.defined(polygon)) {
        floatingPoint.position.setValue(newPosition);
        // 刪除多邊形頂點(diǎn)中最新的一個(gè)點(diǎn)
        activeShapePoints.pop();
        // 將最新獲取到的點(diǎn)添加到多邊形頂點(diǎn)集合中
        activeShapePoints.push(newPosition);
      }
      if (activeShapePoints.length === 3) {
        polygon.polygon.heightReference = Cesium.HeightReference.CLAMP_TO_GROUND;
      }
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
    handler.setInputAction(() => {
      // 刪除最后一個(gè)點(diǎn)(重復(fù)添加的點(diǎn))
      activeShapePoints.pop();
      polygon.pottingPoint = activeShapePoints;
      viewer.entities.remove(floatingPoint);
      floatingPoint = undefined;
      // 銷毀右擊事件
      handler.destroy();
      if (typeof callback === "function") callback(polygon);
    }, Cesium.ScreenSpaceEventType.RIGHT_DOWN);
  };
  export default DrawPolygon;

使用方法

DrawPolygon(this.viewer, {}, (polygon) => {
    console.log(polygon)
})
效果圖
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末注益,一起剝皮案震驚了整個(gè)濱河市碴巾,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌丑搔,老刑警劉巖厦瓢,帶你破解...
    沈念sama閱讀 218,284評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件提揍,死亡現(xiàn)場離奇詭異,居然都是意外死亡煮仇,警方通過查閱死者的電腦和手機(jī)劳跃,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來浙垫,“玉大人刨仑,你說我怎么就攤上這事〖欣眩” “怎么了杉武?”我有些...
    開封第一講書人閱讀 164,614評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長辙售。 經(jīng)常有香客問我轻抱,道長,這世上最難降的妖魔是什么旦部? 我笑而不...
    開封第一講書人閱讀 58,671評(píng)論 1 293
  • 正文 為了忘掉前任祈搜,我火速辦了婚禮,結(jié)果婚禮上志鹃,老公的妹妹穿的比我還像新娘夭问。我一直安慰自己,他們只是感情好曹铃,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,699評(píng)論 6 392
  • 文/花漫 我一把揭開白布缰趋。 她就那樣靜靜地躺著,像睡著了一般陕见。 火紅的嫁衣襯著肌膚如雪秘血。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,562評(píng)論 1 305
  • 那天评甜,我揣著相機(jī)與錄音灰粮,去河邊找鬼。 笑死忍坷,一個(gè)胖子當(dāng)著我的面吹牛粘舟,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播佩研,決...
    沈念sama閱讀 40,309評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼柑肴,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了旬薯?” 一聲冷哼從身側(cè)響起晰骑,我...
    開封第一講書人閱讀 39,223評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎绊序,沒想到半個(gè)月后硕舆,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體秽荞,經(jīng)...
    沈念sama閱讀 45,668評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,859評(píng)論 3 336
  • 正文 我和宋清朗相戀三年抚官,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了扬跋。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,981評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡耗式,死狀恐怖胁住,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情刊咳,我是刑警寧澤彪见,帶...
    沈念sama閱讀 35,705評(píng)論 5 347
  • 正文 年R本政府宣布,位于F島的核電站娱挨,受9級(jí)特大地震影響余指,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜跷坝,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,310評(píng)論 3 330
  • 文/蒙蒙 一酵镜、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧柴钻,春花似錦淮韭、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至毫蚓,卻和暖如春占键,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背元潘。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評(píng)論 1 270
  • 我被黑心中介騙來泰國打工畔乙, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人翩概。 一個(gè)月前我還...
    沈念sama閱讀 48,146評(píng)論 3 370
  • 正文 我出身青樓牲距,卻偏偏與公主長得像,于是被迫代替她去往敵國和親钥庇。 傳聞我的和親對(duì)象是個(gè)殘疾皇子牍鞠,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,933評(píng)論 2 355

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