騰訊地圖實現(xiàn)自定義icon浪蹂,折疊點(diǎn)自動歸整為正數(shù)

最終實現(xiàn)效果铃彰,相同的類型就會折疊:


效果圖.png

js代碼

    creatMap() {
      // 定義地圖默認(rèn)中心點(diǎn)坐標(biāo)
      var center = new TMap.LatLng(22.971870333, 113.373452833);
      //創(chuàng)建map對象晌区,初始化地圖
      var map = new TMap.Map("container", {
        center: center, //設(shè)置地圖中心點(diǎn)坐標(biāo)
        zoom: 16 //設(shè)置地圖縮放級別
      });
      this.map = map;
      this.markers = [];
      this.creatMarkerCluster("Battery", "1");
      this.creatMarkerCluster("Bicycle", "2");
      this.creatMarkerCluster("Cabinet", "3");
      // this.creatRegion();  生成多邊形區(qū)域代碼
      // this.creatPark();  生成多邊形區(qū)域代碼
    },
    // 生成點(diǎn)聚焦函數(shù)
    creatMarkerCluster(type, deviceType) {
      let imgIconList = new Array(3);
      imgIconList[0] = imgIcon1;
      imgIconList[1] = imgIcon2;
      imgIconList[2] = imgIcon3;
      // debugger
      let map = this.map;
      this.markers = [];
      // 1忌傻、標(biāo)點(diǎn)
      let markerCluster = new TMap.MarkerCluster({
        id: "container" + type,
        map: map, //指定地圖容器
        enableDefaultStyle: false, // 啟用默認(rèn)樣式
        minimumClusterSize: 2,
        //點(diǎn)標(biāo)記數(shù)據(jù)數(shù)組
        geometries: this.convertMarkers(deviceType), //this.markers
        zoomOnClick: true, // 點(diǎn)擊簇時放大至簇內(nèi)點(diǎn)分離
        gridSize: 60, // 聚合算法的可聚合距離
        averageCenter: true, // 每個聚和簇的中心是否應(yīng)該是聚類中所有標(biāo)記的平均值
        maxZoom: 25 // 采用聚合策略的最大縮放級別
      });

      var clusterBubbleList = [];
      var markerGeometries = [];
      var marker = null;

      // 監(jiān)聽聚合簇變化
      markerCluster.on("cluster_changed", function(e) {
        // 銷毀舊聚合簇生成的覆蓋物
        if (clusterBubbleList.length) {
          clusterBubbleList.forEach(function(item) {
            item.destroy();
          });
          clusterBubbleList = [];
        }
        markerGeometries = [];

        // 根據(jù)新的聚合簇數(shù)組生成新的覆蓋物和點(diǎn)標(biāo)記圖層
        // debugger
        var clusters = markerCluster.getClusters();
        clusters.forEach((item,index) => {
          console.log(clusters,'clusters')
          if (item.geometries.length > 1) {
            let clusterBubble = new ClusterBubble({
              map,
              position: item.center,
              content: item.geometries.length
            });
            clusterBubble.on("click", () => {
              map.fitBounds(item.bounds);
            });
            clusterBubbleList.push(clusterBubble);
          } else {
            markerGeometries.push({
              position: item.center,
              properties: {
                //自定義屬性
                title: `marker${index}`,
                sn: item.geometries[0].properties.sn
              }
            });
          }
        });
        if (marker) {
          // 已創(chuàng)建過點(diǎn)標(biāo)記圖層沉唠,直接更新數(shù)據(jù)
          marker.setGeometries(markerGeometries);
        } else {
          // 創(chuàng)建點(diǎn)標(biāo)記圖層
          marker = new TMap.MultiMarker({
            map,
            styles: {
              default: new TMap.MarkerStyle({
                width: 34,
                height: 42,
                anchor: {
                  x: 17,
                  y: 21
                },
                src: imgIconList[+deviceType - 1]
              })
            },
            geometries: markerGeometries
          });
          //初始化infoWindow
          var infoWindow = new TMap.InfoWindow({
            map: map,
            position: new TMap.LatLng(20, 110),
            offset: { x: -5, y: -32 } //設(shè)置信息窗相對position偏移像素
          });
          // 監(jiān)聽標(biāo)注點(diǎn)擊事件
          marker.on("click", function(evt) {
            // console.log(evt, "1111");
            //設(shè)置infoWindow
            infoWindow.open(); //打開信息窗
            infoWindow.setPosition(evt.geometry.position); //設(shè)置信息窗位置
            infoWindow.setContent(evt.geometry.properties.sn); //設(shè)置信息窗內(nèi)容
          });
        }
      });
      // 以下代碼為基于DOMOverlay實現(xiàn)聚合點(diǎn)氣泡
      function ClusterBubble(options) {
        TMap.DOMOverlay.call(this, options);
      }

      ClusterBubble.prototype = new TMap.DOMOverlay();

      ClusterBubble.prototype.onInit = function(options) {
        this.content = options.content;
        this.position = options.position;
      };

      // 銷毀時需要刪除監(jiān)聽器
      ClusterBubble.prototype.onDestroy = function() {
        this.dom.removeEventListener("click", this.onClick);
        this.removeAllListeners();
      };

      ClusterBubble.prototype.onClick = function() {
        this.emit("click");
      };

      // 創(chuàng)建氣泡DOM元素
      ClusterBubble.prototype.createDOM = function() {
        var dom = document.createElement("div");
        dom.classList.add(`clusterBubble${type}`);
        dom.innerText = this.content;
        dom.style.cssText = [
          "width: " + 40 + "px;",
          "height: " + 40 + "px;",
          "line-height: " + 40 + "px;"
        ].join(" ");

        // 監(jiān)聽點(diǎn)擊事件疆虚,實現(xiàn)zoomOnClick
        this.onClick = this.onClick.bind(this);
        // pc端注冊click事件,移動端注冊touchend事件
        dom.addEventListener("click", this.onClick);
        return dom;
      };
      ClusterBubble.prototype.updateDOM = function() {
        if (!this.map) {
          return;
        }
        // 經(jīng)緯度坐標(biāo)轉(zhuǎn)容器像素坐標(biāo)
        let pixel = this.map.projectToContainer(this.position);

        // 使文本框中心點(diǎn)對齊經(jīng)緯度坐標(biāo)點(diǎn)
        let left = pixel.getX() - this.dom.clientWidth / 2 + "px";
        let top = pixel.getY() - this.dom.clientHeight / 2 + "px";
        this.dom.style.transform = `translate(${left}, ${top})`;

        this.emit("dom_updated");
      };
    },
    // 生成點(diǎn)的函數(shù)
    convertMarkers(type) {
      let markers = [];
      this.workbenchList.map((item, index) => {   // this.workbenchList起始數(shù)據(jù)
        if (item.lat && item.lng && item.lat !== "0" && !item.lng !== "0") {
          if (item.deviceType === type) {
            markers.push({
              id: item.id, //點(diǎn)標(biāo)記唯一標(biāo)識满葛,后續(xù)如果有刪除径簿、修改位置等操作,都需要此id
              styleId: "myStyle", //指定樣式id
              position: new TMap.LatLng(item.lat, item.lng), //點(diǎn)標(biāo)記坐標(biāo)位置
              properties: {
                //自定義屬性
                title: `marker${index}`,
                sn: "設(shè)備編號:" + item.sn
              }
            });
          }
        }
      });
      return markers;
    },
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末嘀韧,一起剝皮案震驚了整個濱河市篇亭,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌锄贷,老刑警劉巖译蒂,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異谊却,居然都是意外死亡柔昼,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進(jìn)店門炎辨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來捕透,“玉大人,你說我怎么就攤上這事〖ぢ剩” “怎么了咳燕?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長乒躺。 經(jīng)常有香客問我招盲,道長,這世上最難降的妖魔是什么嘉冒? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任曹货,我火速辦了婚禮,結(jié)果婚禮上讳推,老公的妹妹穿的比我還像新娘顶籽。我一直安慰自己,他們只是感情好银觅,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布礼饱。 她就那樣靜靜地躺著,像睡著了一般究驴。 火紅的嫁衣襯著肌膚如雪镊绪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天洒忧,我揣著相機(jī)與錄音蝴韭,去河邊找鬼。 笑死熙侍,一個胖子當(dāng)著我的面吹牛榄鉴,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播蛉抓,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼庆尘,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了巷送?” 一聲冷哼從身側(cè)響起驶忌,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎惩系,沒想到半個月后位岔,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡堡牡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年抒抬,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片晤柄。...
    茶點(diǎn)故事閱讀 39,690評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡擦剑,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情惠勒,我是刑警寧澤赚抡,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站纠屋,受9級特大地震影響涂臣,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜售担,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一赁遗、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧族铆,春花似錦岩四、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至逝淹,卻和暖如春耕姊,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背创橄。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工箩做, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留莽红,地道東北人妥畏。 一個月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像安吁,于是被迫代替她去往敵國和親醉蚁。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評論 2 353

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