高德地图开发(四、点聚合)

一、设置点聚合

只截取了部分代码,几个方法都处于一个类中。

let markers = []; // 点聚合数组
for (let i = 0; i < 10; i++) {
     customMark();
 }
 initMarkerClusterer();
 cluster.setMaxZoom(15);
 
// 添加自定义标记
function customMark () {
    let lonlat = [Math.random() + 113, Math.random() + 23];
    // 创建 AMap.Icon 实例:
    let icon = new AMap.Icon({
        size: new AMap.Size(58, 70),    // 图标尺寸
        image: 'http://localhost:8080/static/img/patrol.png',  // Icon的图像
        imageSize: new AMap.Size(58, 70)   // 根据所设置的大小拉伸或压缩图片
    });

    // 将 Icon 实例添加到 marker 上:
    let marker = new AMap.Marker({
        position: new AMap.LngLat(lonlat[0], lonlat[1]),
        offset: new AMap.Pixel(-10, -10),
        icon: icon, // 添加 Icon 实例
        title: '自定义图标',
        zoom: 13

    });
    map.add(marker); 
    markers.push(marker);
}
// 初始化点聚合
 function initMarkerClusterer () {
   //添加聚合组件
    map.plugin(["AMap.MarkerClusterer"],function() {
        cluster = new AMap.MarkerClusterer(
            map,     // 地图实例
            markers);
        cluster.setMaxZoom(12);
    });
}

// 添加点标记至点聚合中
this.addMarkerClusterer = function () {
    let lonlat = [Math.random() + 113, Math.random() + 23];
    // 创建 AMap.Icon 实例:
    let icon = new AMap.Icon({
        size: new AMap.Size(58, 70),    // 图标尺寸
        image: 'http://localhost:8080/static/img/patrol.png',  // Icon的图像
        imageSize: new AMap.Size(58, 70)   // 根据所设置的大小拉伸或压缩图片
    });

    // 将 Icon 实例添加到 marker 上:
    let marker = new AMap.Marker({
        position: new AMap.LngLat(lonlat[0], lonlat[1]),
        offset: new AMap.Pixel(-10, -10),
        icon: icon, // 添加 Icon 实例
        title: '自定义图标',
        zoom: 13
    });
    markerEvent(marker, lonlat);
    cluster.addMarker(marker);
}

在这里插入图片描述

二、点聚合中添加删除标记

// 添加点标记至点聚合中
this.addMarkerClusterer = function () {
    let lonlat = [Math.random() + 113, Math.random() + 23];
    // 创建 AMap.Icon 实例:
    let icon = new AMap.Icon({
        size: new AMap.Size(58, 70),    // 图标尺寸
        image: 'http://localhost:8080/static/img/patrol.png',  // Icon的图像
        imageSize: new AMap.Size(58, 70)   // 根据所设置的大小拉伸或压缩图片
    });

    // 将 Icon 实例添加到 marker 上:
    let marker = new AMap.Marker({
        position: new AMap.LngLat(lonlat[0], lonlat[1]),
        offset: new AMap.Pixel(-10, -10),
        icon: icon, // 添加 Icon 实例
        title: '自定义图标',
        zoom: 13
    });
    markerEvent(marker, lonlat);
    cluster.addMarker(marker);
}
	// 删除点标记
    cluster.removeMarker(marker);

三、方法和事件

https://lbs.amap.com/api/javascript-api/reference/plugin#AMap.MarkerClusterer
https://lbs.amap.com/api/javascript-api/reference/plugin#AMap.MarkerClusterer

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值