高德地图2.0 点聚合 点击 Maker显示信息窗体和自己的数据

文章介绍了如何在Vue3和TypeScript项目中使用高德地图2.0的点聚合功能,展示了如何解决点击marker时显示对应信息窗体的问题。通过自定义聚合点和非聚合点的样式,并将数据存储在`extData`中,实现了点击maker时弹出包含详细信息的窗体。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.vue3+ts 使用高德地图2.0 点聚合  单击maker 显示信息窗体  解决无法 maker 和 需要的信息 相对应问题 

2.直接上代码  效果图在下方

 AMapLoader.load({
        key: "", // 申请好的Web端开发者Key,首次调用 load 时必填
        version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
        plugins: [
            "AMap.ToolBar",
            "AMap.Scale",
            "AMap.MapType",
            'AMap.PlaceSearch',
            'AMap.AutoComplete',
            'AMap.MouseTool',
            'AMap.Circle',
            'AMap.MarkerCluster'
        ],
    })
        .then((AMap) => {

            map = new AMap.Map("map_box", {
                //设置地图容器id
                viewMode: "3D", //是否为3D地图模式
                zoom: 6, //初始化地图级别
                center: locationArr.value, //初始化地图中心点位置
                // mapStyle: "amap://styles/270733bbb12c36d9bd368103b6a9e244"
                mapStyle: "amap://styles/darkblue"
            });
            let lineArr: any = []
            let LngLatArray2: any = [];
            // lineArr2 = []
            var size: number = 40;
            var convertNum = 0;
            var count = state.car_data.length;

            //聚合点样式
            var _renderClusterMarker = function (context) {
                let data: any = context.clusterData[0].extData
                var div = document.createElement("div");
                var fontColor = "hsla(120,50%,0%,1)";
                div.style.width = "37px";
                div.style.height = "46px";
                div.innerHTML = context.count;
                div.style.lineHeight = size + "px";
                div.style.color = fontColor;
                div.style.fontSize = "14px";
                div.style.textAlign = "center";
               //自定义显示maker  的图标
                div.style.backgroundImage = "url(自己图片的地址 网图)"
                context.marker.setOffset(new AMap.Pixel(-size / 2, -size / 2));
                context.marker.setContent(div);
                context.marker.on('click', function (e) {
                    var curZoom = map.getZoom();
                    if (curZoom < 20) {
                        curZoom += 1;
                    }
                    map.setZoomAndCenter(curZoom, e.lnglat);
                });
            };
            //非聚合点样式
            var _renderMarker = function (context) {
                let data: any = context.data[0].extData
                let content = '<div style=" height: 35px;width:30px"><img src="http://file_upload.suntaeiv.com:50000/file_management/db/bc267af5b56e6b09a9263c1f9cab70cc0b814d.png" style="width: 100%;height:100%"/></div>';
                    context.marker.setLabel({
                        direction: 'bottom',
                        offset: new AMap.Pixel(0, 0),  //设置文本标注偏移量
                        content: "<div class='info'> <span class='info_right'>" + data.license_plate_number + "    " + "</span><span>" + "    " + data.car_speed + 'km/h' + "</span></div>", //设置文本标注内容
                    });
                    context.marker.on('click', function () {
                        var info: any = [];
                        info.push("<div style='box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);'>")
                        info.push("<div style='height: 35px;display: flex; width: 400px; align-items: center; justify-content: space-between;background-color: #4997cf;color: aliceblue;'>")
                        info.push("<div style='margin-left: 10px;'>" + data.license_plate_number + "</div>")
                        info.push("<div style='margin-right: 10px;'><el-icon> <Close /></el-icon></div></div>")
                        info.push("<div style='display: flex; width: 400px;'>")
                        info.push("<div style='font-size: 11px;color:#357abd;height: 150px;width: 130px;'>")
                        info.push("<div class='form_info_font'> 车辆名称 </div> <div class='form_info_font'> 车牌号码 </div> <div class='form_info_font'> VIN码 </div> <div class='form_info_font'>车辆状态</div>  <div class='form_info_font'> 定位时间 </div><div class='form_info_font'> 行驶速度 </div> </div>")
                        info.push("<div style='font-size: 11px;color:#000;height: 150px;width: 170px;'>  <div class='form_info_font1'> " + data.car_name + "</div> <div class='form_info_font1'>" + data.license_plate_number + " </div> <div class='form_info_font1'>" + data.vin + "</div> <div class='form_info_font1'> " + data.online_status + "</div>  <div class='form_info_font1'>" + data.positioning_time + " </div> <div class='form_info_font1'> " + data.car_speed + "km/h </div> </div>")
                        info.push("<div style='background-color: #f1f1f1;height: 150px;'>")
                        info.push("<img src='http://116.62.42.241/gps-web/rs/img/car/bigs/%E8%BD%BF%E8%BD%A6/1.png' style='width: 100%;height:100%'/></div>")
                        // info.push("</div><div style='font-size:11px; height:30px;display:flex;align-items: center;'><div onclick='win_info(1)' style='margin-left:10px'>轨迹</div><div onclick='win_info(2)' style='margin-left:10px'>详情</div><div onclick='win_info(3)' style='margin-left:10px'>跟踪车辆</div> </div></div>")
                        infoWindow = new AMap.InfoWindow({
                            offset: new AMap.Pixel(0, -30),
                            content: info.join("")  //使用默认信息窗体框样式,显示信息内容
                        });
                        infoWindow.open(map, context.marker.getPosition());
                    });
                
                var offset = new AMap.Pixel(-9, -9);
                context.marker.setContent(content);
                context.marker.setOffset(offset);

            };
            const MarkerCluster = (data: any) => {
                var gridSize = 40;
                var cluster = new AMap.MarkerCluster(map, data, {
                    gridSize: gridSize,
                    renderClusterMarker: _renderClusterMarker, // 自定义聚合点样式
                    renderMarker: _renderMarker, // 自定义非聚合点样式
                });
            };

          //处理数据 从接口获得的数据 拿出 经纬度 
            for (let i = 0; i < state.car_data.length; i++) {
                lat = state.car_data[i].latitude
                lng = state.car_data[i].longitude
                if (lat != '0') {
                    lineArr.push(new AMap.LngLat(lng, lat))
                }

            }
// 进行批量 gps 转高德坐标点
            let pageNum = parseInt((lineArr.length + size - 1) / size + '');
            const convert = (LngLatArray: any) => {
                AMap.convertFrom(LngLatArray, 'gps', function (status: any, result: any) {
                    if (result.info === "ok" && status == 'complete') {
                        var resLnglat = result.locations[0];
                        console.log(result.locations.length);

//这里是重点  extData 把你要在maker 上显示的数据 
//塞在里面 1对1 对应好 这里赋空值在下面赛   
                        for (let index = 0; index < result.locations.length; index++) {
                            LngLatArray2.push({
                                weight: '',
                                lnglat: [result.locations[index].lng, result.locations[index].lat],
                                extData: ""
                            })
                        }
                        convertNum++;
                        if (convertNum >= pageNum) {
                            for (let index = 0; index < LngLatArray2.length; index++) {
                                LngLatArray2[index].extData = state.car_data[index];
                            }
                            // const combinedArr = LngLatArray2.concat(factoryArray, hydrogenArray);
                            MarkerCluster(LngLatArray2);
                        }

                    }
                });
            }
            for (var i = 0; i < pageNum; i++) {
                var LngLatArraySlice = lineArr.slice(i * size, (i + 1) * size);
                convert(LngLatArraySlice);
            }

        })

}

3.这里重点分析 讲解一下 

高德地图api2.0 以后 不能MarkerCluster 只能传入

不再像以前可以传 maker 数组,可以把对应数据的标识赛在maker 的title中 ,相当于添加一个唯一标识,然后去匹配数据,而是只能塞入上方图片的数组,所以无法做到点击maker 拿到该maker对应的信息,所以有了新的数据形式

 构建这样一个数组 weight是权重,这里没用到就不解释了,extData 可以存入你要显示的数据,

然后自定义聚合点样式

           let data: any = context.clusterData[0].extData

和非聚合点样式 

let data: any = context.data[0].extData

这样就可以获取到每个maker 中存入相对应的数据进行  信息窗体的显示 

 

 3.自定义点聚合的样式

 

 4.点击maker 显示窗体 显示自己需要显示的信息

 

 

高德地图maker原理是通过在地图上添加标记来标识特定的位置或地。在高德地图的开发中,可以使用AMap.Marker类来创建一个maker对象,并通过设置maker的位置、图标等属性来自定义标记的样式信息。通过调用地图对象的add方法将maker添加到地图上,即可在地图上显示出相应的标记。 引用\[2\]中提到的高德地图的基础定位功能的代码示例中,可以看到在JavaScript中创建了一个地图对象map,并通过new AMap.Map方法指定了地图的显示容器一些基本的配置参数。然后通过new AMap.Marker方法创建了一个maker对象,并设置了maker的位置信息。最后通过调用地图对象的add方法将maker添加到地图上,从而实现了在地图上显示一个标记。 需要注意的是,高德地图maker原理是基于JavaScript的,通过在前端页面中使用JavaScript代码来实现。在具体的开发过程中,可以根据自己的需求实际情况,通过设置maker的属性事件来实现更多的功能,比如点击标记时的弹窗信息、自定义图标样式等。 总结起来,高德地图maker原理是通过在地图上添加标记来标识特定的位置或地,通过设置maker的属性事件来实现不同的功能。可以通过JavaScript代码来创建配置maker对象,并通过地图对象的add方法将maker添加到地图上。 #### 引用[.reference_title] - *1* *2* [高德地图API开发应用-----地图显示+定位+marker](https://blog.csdn.net/sherry_unique/article/details/53841653)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [android 高德地图多个marker带图显示_GPS原理与编程实现高德地图](https://blog.csdn.net/weixin_39944146/article/details/110346480)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值