高德amap 自定义点聚合

<template>

  <div class="box">

    <div id="container" style="width:100%; height:100%"></div>

  </div>

</template>

<script>

//import AMap from "AMap"; // 引入高德地图

export default {

  props: {

    nowOrgData: {

      type: Object,

      default: []

    }

  },

  data() {

    return {

      map: [],

      infoWindow: [],

      proviceInfo: [],

      orgData: [],

      leafNum: "",

      domain: this.abdpConfig.getAuther().domain,

      //nowOrgData: [],

      icon: ""

    };

  },

  watch: {

    nowOrgData: function(newvalue, oldvalue) {

      // console.log('orgname-old',oldvalue.domainname)

      // console.log('orgname-new',newvalue.domainname)

      this.initMarker(newvalue);

    }

  },

  created() {

    this.org = this.abdpConfig.getOrg();

    this.orgData = this.org[0] == undefined ? this.org : this.org[0];

    this.infoWindow = new AMap.InfoWindow({

      offset: new AMap.Pixel(20, -20),

       isCustom:true

    });

  },

  mounted() {

    this.init();

    this.initMarker(this.nowOrgData);

    this.getCity(this.nowOrgData);

    //  this.renderDistrict(this.nowOrgData);

  },

  methods: {

    init() {

      this.map = new AMap.Map("container", {

        resizeEnable: true,

        zoom: 7,

        center: [108, 34],

        pitch: 0,

        viewMode: "2D",

        mapStyle: "amap://styles/blue",

        plugin: [

          "AMap.Geocoder",

          "AMap.Geolocation",

          "AMap.Autocomplete",

          "AMap.Polygon",

          "AMap.DistrictSearch",

          "AMap.CircleMarker",

          "AMap.Scale",

          "AMap.OverView",

          "AMap.ToolBar",

          "AMap.MapType",

          "AMap.PolyEditor",

          "AMap.CircleEditor"

        ]

      });

    },

    initMarker(nowOrgData) {

      //this.abdpConfig.setNowdomain(nowOrgData.domainname);

      let self = this;

      self.map.clearMap();

      self.map.clearInfoWindow();

      let index = -1;

      if (typeof nowOrgData.gps == "string") {

        self.map.setCenter(nowOrgData.gps.split(","));

        self.map.setZoom(nowOrgData.gps.zoom);

        //  console.log(self.map.getCenter(),self.map.getZoom())

      }

      while (++index < nowOrgData.childrenDomains.length) {

        if (typeof nowOrgData.childrenDomains[index].gps == "string") {

          nowOrgData.childrenDomains[index].gps = nowOrgData.childrenDomains[

            index

          ].gps.split(",");

        }

        this.leafNum = 0;

        let marker = new AMap.Marker({

          extData: nowOrgData.childrenDomains[index],

          zoom: nowOrgData.childrenDomains[index].zoom,

          position: nowOrgData.childrenDomains[index].gps,

          content: self.renderContent(nowOrgData.childrenDomains[index])

        });

        self.map.add(marker);

        marker.on("click", self.markerClick);

        marker.on("mouseover", self.markerMouseover);

        marker.on("mouseout", self.markerMouseout);

      }

      self.map.center = self.map.getCenter();

      self.map.setFitView();

      self.renderDistrict(self.proviceInfo);

    },

    markerClick(e) {

      let self = this;

      if (e.target.B.extData.childrenDomains == undefined) {

        console.log( e.target.B.extData.domainname)

        this.$router.push({

          path: "./systemMap/LeafMap",

          query: {

            domain: e.target.B.extData.domainname

          }

        });

      }else{

        self.initMarker(e.target.getExtData());

        self.go(e.target.getExtData());

      }

    },

    go(domainData) {

      this.$emit("updatdomainname", domainData);

    },

    markerMouseover(e) {

      let org = e.target.getExtData();

      let content =

        '<div class="infowindow" style="padding:5px;background:rgb(150,150,150,0.7); border-radius: 5px;box-shadow: rgba(30, 30, 30, 0.298039) 0px 1px 4px -1px;color:white;width:160px;font-size:12px;list-style:none;"><ul ><li><h4 style="color:orange">' +

        org.domainname +

        "<h4></li>";

      content += "<li>已安装设备:" + org.ispDevice + "</li>";

      content +=

        "<li>正常设备:" +

        org.normalIspDevice +

        '</li><ul><div class="to_bottom" style=" width: 0;height:0;    box-shadow: rgba(0, 0, 0, 0.298039) 0px 1px 4px -1px;border-top: 10px solid rgb(150,150,150,0.5);border-left: 10px solid transparent;border-right: 10px solid transparent; border-bottom: 10px solid transparent;position:absolute;bottom:-12px;left:80px" ></div></div>';

      this.infoWindow.setContent(content);

      this.infoWindow.open(this.map, e.target.getPosition());

    },

    markerMouseout(e) {

      this.infoWindow.close(this.map, e.target.getPosition());

    },

    renderContent(nowOrgData) {

      let content = "";

      let leafNum = "";

      if (nowOrgData.childrenDomains == undefined) {

        content =

          '<img src="./static/images/mapmarker/map-marker-blue.png" style="z-index: 1;" />';

        return content;

      } else {

        leafNum = this.getLeafNum(nowOrgData);

        return this._renderClusterMarker(leafNum);

      }

    },

 

    getLeafNum: function(org) {

      if (org) {

        if (org.isLeaf) {

          this.leafNum++;

        } else {

          if (org.childrenDomains && org.childrenDomains.length) {

            for (let i = 0; i < org.childrenDomains.length; i++) {

              this.getLeafNum(org.childrenDomains[i]);

            }

          }

        }

      }

      return this.leafNum;

    },

    _renderClusterMarker(leafNum) {

      // let factor = Math.pow(leafNum, 1 / 18);

      // let div = document.createElement("div");

      // let Hue = 30 - factor * 180;

      // let bgColor = "hsla(" + Hue + ",100%,50%,0.7)";

      // let fontColor = "hsla(" + Hue + ",100%,20%,1)";

      // let borderColor = "hsla(" + Hue + ",100%,40%,1)";

      // let shadowColor = "hsla(" + Hue + ",100%,50%,1)";

      // div.style.backgroundColor = bgColor;

      // let size = Math.round(30 + Math.pow(leafNum, 1 / 5) * 10);

      // div.style.width = div.style.height = size + "px";

      // div.style.border = "solid 1px " + borderColor;

      // div.style.borderRadius = size / 2 + "px";

      // div.style.boxShadow = "0 0 1px " + shadowColor;

      // div.innerHTML = leafNum;

      // div.style.lineHeight = size + "px";

      // div.style.color = fontColor;

      // div.style.fontSize = "14px";

      // div.style.color = "white";

      // div.style.textAlign = "center";

      

      var div = '';

      if(leafNum>0 && leafNum < 10)

      {

          div = '<div style="width: 68px;height: 68px;  -moz-background-size: 100% 100%;background-size: 100% 100%; background-image: url(&quot;./static/images/ClusterImg/m1.png&quot;); text-align: center; line-height: 68px; background-position: 0px 0px; color: rgb(0, 0, 0); font-size: 12px; cursor: pointer; color :white;">'+leafNum+'</div>'

      }else if(leafNum>=10&& leafNum < 50){

     div = '<div style="width: 80px; height: 80px;  -moz-background-size: 100% 100%;background-size: 100% 100%; background-image: url(&quot;./static/images/ClusterImg/m2.png&quot;); text-align: center; line-height: 80px; background-position: 0px 0px; color: rgb(0, 0, 0); font-size: 12px; cursor: pointer;">'+leafNum+'</div>'

      }else if(leafNum>=50&& leafNum < 100){

           div = '<div style="width: 100px; height: 100px; -moz-background-size: 100% 100%;background-size: 100% 100%; background-image: url(&quot;./static/images/ClusterImg/m3.png&quot;); text-align: center; line-height: 100px; background-position: 0px 0px; color: rgb(0, 0, 0); font-size: 12px; cursor: pointer;color :white;">'+leafNum+'</div>'

      }else if(leafNum>=100&& leafNum <500){

          //div.style.background =  'url("./static/images/ClusterImg/m5.png")'

          div = '<div style="width: 120px; height: 120px;  -moz-background-size: 100% 100%;background-size: 100% 100%; background-image: url(&quot;./static/images/ClusterImg/m4.png&quot;); text-align: center; line-height: 120px; background-position: 0px 0px; color: rgb(0, 0, 0); font-size: 12px; cursor: pointer;color :white;">'+leafNum+'</div>'

      }else{

           div = '<div style="width: 150px; height: 150px;  -moz-background-size: 100% 100%;background-size: 100% 100%; background-image: url(&quot;./static/images/ClusterImg/m5.png&quot;); text-align: center; line-height: 150px; background-position: 0px 0px; color: rgb(0, 0, 0); font-size: 12px; cursor: pointer;color :white;">'+leafNum+'</div>'

      }

      return div;

    },

    getCity(nowOrgData) {

      if (nowOrgData.domainname == "root") {

        return;

      }

      let self = this;

 

      self.map.getCity(function(info) {

        //     console.log(info)

        self.proviceInfo = info;

        self.renderDistrict(info);

      });

    },

    renderBoundary(Code, dep) {

      //省行政

      // console.log(Code,'code0000')

      let disProvince = new AMap.DistrictLayer.Province({

        adcode: [Code],

        depth: dep,

        styles: {

          fill: "#012f8d",

          "province-stroke": "#2db5ff",

          "city-stroke": "#2db5ff", //地级市边界#130e12

          "county-stroke": "#2db5ff" //区县边界#130e12 rgba(19,14,18,1)

        }

      });

      disProvince.setMap(this.map);

    },

    renderDistrict(info) {

      //省遮罩

 

      let Code = "";

      let dep = "";

      let self = this;

      new AMap.DistrictSearch({

        extensions: "all",

        subdistrict: 0

      }).search(info.province, function(status, result) {

        // 外多边形坐标数组和内多边形坐标数组

        Code = result.districtList[0].adcode;

        self.renderBoundary(Code, 2);

        let outer = [

          new AMap.LngLat(-360, 90, true),

          new AMap.LngLat(-360, -90, true),

          new AMap.LngLat(360, -90, true),

          new AMap.LngLat(360, 90, true)

        ];

        let holes = result.districtList[0].boundaries;

        let pathArray = [outer];

        pathArray.push.apply(pathArray, holes);

        let polygon = new AMap.Polygon({

          pathL: pathArray,

          //线条颜色,使用16进制颜色代码赋值。默认值为#006600

          strokeColor: "#2db5ff",

          strokeWeight: 4,

          //轮廓线透明度,取值范围[0,1],0表示完全透明,1表示不透明。默认为0.9

          strokeOpacity: 1,

          //多边形填充颜色,使用16进制颜色代码赋值,如:#FFAA00

          fillColor: "rgba(0,0,0)",

          //多边形填充透明度,取值范围[0,1],0表示完全透明,1表示不透明。默认为0.9

          fillOpacity: 0,

          //轮廓线样式,实线:solid,虚线:dashed

          strokeStyle: "solid"

        });

        polygon.setPath(pathArray);

        self.map.add(polygon);

        // let overlaysList = self.map.getAllOverlays('polygon');//获取多边形图层

        //  self.map.setFitView(polygon);//自适应显示

      });

    }

  }

};

</script>

<style scoped>

   .infowindow{

        padding:5px;

        background:rgb(50,50,50,0.6);

        border-radius: 5px;

        box-shadow: rgba(0, 0, 0, 0.298039) 0px 1px 4px -1px;

        color:white;

    }

    .to_bottom {

    width: 0;

    height:0;    

    box-shadow: rgba(0, 0, 0, 0.298039) 0px 1px 4px -1px;

    border-top: 10px solid rgb(100,100,100,0.8);

    border-left: 10px solid transparent;

    border-right: 10px solid transparent;

    border-bottom: 10px solid transparent;

}

</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要清除高德地图上特定分类的聚合,你可以在创建标记聚合器时,使用 `filter` 参数来指定需要聚合标记的条件。以下是一个示例代码: ```javascript // 创建地图对象 var map = new AMap.Map('mapContainer', { zoom: 13, // 设置地图缩放级别 center: [116.397428, 39.90923] // 设置地图中心 }); // 创建标记 var marker1 = new AMap.Marker({ position: [116.405285, 39.904989], category: 'category1' // 设置标记的分类 }); var marker2 = new AMap.Marker({ position: [116.406285, 39.904989], category: 'category2' // 设置标记的分类 }); var marker3 = new AMap.Marker({ position: [116.407285, 39.904989], category: 'category1' // 设置标记的分类 }); // 创建标记聚合器 var cluster = new AMap.MarkerClusterer(map, [marker1, marker2, marker3], { gridSize: 80, renderCluserMarker: function (context) { // 自定义聚合标记的样式 var count = context.count; var size = Math.sqrt(count) * 10 + 20; return new AMap.TextMarker({ text: count, anchor: 'bottom', offset: new AMap.Pixel(-size / 2, -size / 2), style: { background: '#0088ff', borderColor: '#0088ff', color: '#fff', borderRadius: size / 2 + 'px', width: size + 'px', height: size + 'px', textAlign: 'center', lineHeight: size + 'px' } }); }, // 设置只聚合指定分类的标记 filter: function (marker) { return marker.category === 'category1'; } }); // 清除特定分类的聚合 cluster.setMap(null); ``` 在以上代码中,我们创建了三个标记,并为每个标记指定了一个分类。然后,通过在 `AMap.MarkerClusterer` 的配置中添加 `filter` 参数,设置只聚合 `category1` 分类的标记。最后,使用 `cluster.setMap(null)` 方法将特定分类的聚合从地图上清除。 你可以根据自己的需求修改代码,设置不同的分类和条件来清除不同分类的聚合。同样,确保在使用高德地图 JavaScript API 之前已经加载了 API 库文件,并将 `'mapContainer'` 替换为你自己的地图容器ID。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值